2010-04-10 02:35:36 +00:00
|
|
|
/* MSPDebug - debugging tool for the eZ430
|
|
|
|
* Copyright (C) 2009, 2010 Daniel Beer
|
|
|
|
*
|
|
|
|
* 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 2 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
2011-07-26 13:51:06 +00:00
|
|
|
|
2010-04-10 02:35:36 +00:00
|
|
|
#include "device.h"
|
|
|
|
#include "binfile.h"
|
|
|
|
#include "stab.h"
|
2010-05-01 05:54:10 +00:00
|
|
|
#include "expr.h"
|
2010-08-13 04:40:58 +00:00
|
|
|
#include "reader.h"
|
2010-08-13 03:45:08 +00:00
|
|
|
#include "output_util.h"
|
2010-04-10 02:35:36 +00:00
|
|
|
#include "util.h"
|
2010-09-10 00:52:47 +00:00
|
|
|
#include "prog.h"
|
2010-04-10 02:35:36 +00:00
|
|
|
#include "dis.h"
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int cmd_regs(char **arg)
|
2010-04-10 02:35:36 +00:00
|
|
|
{
|
2010-08-05 02:43:07 +00:00
|
|
|
address_t regs[DEVICE_NUM_REGS];
|
2010-05-13 00:57:21 +00:00
|
|
|
uint8_t code[16];
|
2010-05-13 04:57:32 +00:00
|
|
|
int len = sizeof(code);
|
2010-04-10 02:35:36 +00:00
|
|
|
|
2011-11-14 18:38:19 +00:00
|
|
|
(void)arg;
|
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_getregs(regs) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
2010-08-13 03:45:08 +00:00
|
|
|
show_regs(regs);
|
2010-04-10 02:35:36 +00:00
|
|
|
|
|
|
|
/* Try to disassemble the instruction at PC */
|
2010-05-13 04:57:32 +00:00
|
|
|
if (len > 0x10000 - regs[0])
|
|
|
|
len = 0x10000 - regs[0];
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_readmem(regs[0], code, len) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
return 0;
|
|
|
|
|
2012-10-03 23:10:36 +00:00
|
|
|
disassemble(regs[0], (uint8_t *)code, len, device_default->power_buf);
|
2010-04-10 02:35:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int cmd_md(char **arg)
|
2010-04-10 02:35:36 +00:00
|
|
|
{
|
|
|
|
char *off_text = get_arg(arg);
|
|
|
|
char *len_text = get_arg(arg);
|
2010-08-05 01:59:33 +00:00
|
|
|
address_t offset = 0;
|
|
|
|
address_t length = 0x40;
|
2010-04-10 02:35:36 +00:00
|
|
|
|
|
|
|
if (!off_text) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("md: offset must be specified\n");
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-15 02:20:50 +00:00
|
|
|
if (expr_eval(off_text, &offset) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("md: can't parse offset: %s\n", off_text);
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len_text) {
|
2011-03-15 02:20:50 +00:00
|
|
|
if (expr_eval(len_text, &length) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("md: can't parse length: %s\n",
|
2010-04-10 02:35:36 +00:00
|
|
|
len_text);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-10-11 19:16:28 +00:00
|
|
|
} else if (offset < 0x10000 && offset + length > 0x10000) {
|
2010-04-10 02:35:36 +00:00
|
|
|
length = 0x10000 - offset;
|
|
|
|
}
|
|
|
|
|
2010-10-11 23:48:25 +00:00
|
|
|
reader_set_repeat("md 0x%x 0x%x", offset + length, length);
|
|
|
|
|
2010-04-10 02:35:36 +00:00
|
|
|
while (length) {
|
2011-02-20 22:30:32 +00:00
|
|
|
uint8_t buf[4096];
|
2010-04-10 02:35:36 +00:00
|
|
|
int blen = length > sizeof(buf) ? sizeof(buf) : length;
|
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_readmem(offset, buf, blen) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
2010-08-13 03:45:08 +00:00
|
|
|
hexdump(offset, buf, blen);
|
2010-04-10 02:35:36 +00:00
|
|
|
|
|
|
|
offset += blen;
|
|
|
|
length -= blen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int cmd_mw(char **arg)
|
2010-04-10 02:35:36 +00:00
|
|
|
{
|
|
|
|
char *off_text = get_arg(arg);
|
|
|
|
char *byte_text;
|
2010-08-05 01:59:33 +00:00
|
|
|
address_t offset = 0;
|
|
|
|
address_t length = 0;
|
2010-05-13 00:57:21 +00:00
|
|
|
uint8_t buf[1024];
|
2010-04-10 02:35:36 +00:00
|
|
|
|
|
|
|
if (!off_text) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("md: offset must be specified\n");
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-15 02:20:50 +00:00
|
|
|
if (expr_eval(off_text, &offset) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("md: can't parse offset: %s\n", off_text);
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((byte_text = get_arg(arg))) {
|
|
|
|
if (length >= sizeof(buf)) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("md: maximum length exceeded\n");
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[length++] = strtoul(byte_text, NULL, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!length)
|
|
|
|
return 0;
|
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_writemem(offset, buf, length) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int cmd_reset(char **arg)
|
2010-04-10 02:35:36 +00:00
|
|
|
{
|
2011-11-14 18:38:19 +00:00
|
|
|
(void)arg;
|
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
return device_ctl(DEVICE_CTL_RESET);
|
2010-04-10 02:35:36 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int cmd_erase(char **arg)
|
2010-04-10 02:35:36 +00:00
|
|
|
{
|
2010-09-23 04:08:45 +00:00
|
|
|
const char *type_text = get_arg(arg);
|
|
|
|
const char *seg_text = get_arg(arg);
|
|
|
|
device_erase_type_t type = DEVICE_ERASE_MAIN;
|
|
|
|
address_t segment = 0;
|
|
|
|
|
2011-03-15 02:20:50 +00:00
|
|
|
if (seg_text && expr_eval(seg_text, &segment) < 0) {
|
2010-09-23 04:08:45 +00:00
|
|
|
printc_err("erase: invalid expression: %s\n", seg_text);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type_text) {
|
|
|
|
if (!strcasecmp(type_text, "all")) {
|
|
|
|
type = DEVICE_ERASE_ALL;
|
|
|
|
} else if (!strcasecmp(type_text, "segment")) {
|
|
|
|
type = DEVICE_ERASE_SEGMENT;
|
|
|
|
if (!seg_text) {
|
|
|
|
printc_err("erase: expected segment "
|
|
|
|
"address\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printc_err("erase: unknown erase type: %s\n",
|
|
|
|
type_text);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_ctl(DEVICE_CTL_HALT) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Erasing...\n");
|
2011-03-15 00:12:48 +00:00
|
|
|
return device_erase(type, segment);
|
2010-04-10 02:35:36 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int cmd_step(char **arg)
|
2010-04-10 02:35:36 +00:00
|
|
|
{
|
|
|
|
char *count_text = get_arg(arg);
|
2010-10-11 22:26:36 +00:00
|
|
|
address_t count = 1;
|
|
|
|
int i;
|
2010-04-10 02:35:36 +00:00
|
|
|
|
2010-10-11 22:26:36 +00:00
|
|
|
if (count_text) {
|
2011-03-15 02:20:50 +00:00
|
|
|
if (expr_eval(count_text, &count) < 0) {
|
2010-10-11 22:26:36 +00:00
|
|
|
printc_err("step: can't parse count: %s\n", count_text);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2010-04-10 02:35:36 +00:00
|
|
|
|
2010-10-11 22:26:36 +00:00
|
|
|
for (i = 0; i < count; i++)
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_ctl(DEVICE_CTL_STEP) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-10-11 23:48:25 +00:00
|
|
|
reader_set_repeat("step");
|
2010-08-13 04:40:58 +00:00
|
|
|
return cmd_regs(NULL);
|
2010-04-10 02:35:36 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int cmd_run(char **arg)
|
2010-04-10 02:35:36 +00:00
|
|
|
{
|
|
|
|
device_status_t status;
|
2010-08-05 02:43:07 +00:00
|
|
|
address_t regs[DEVICE_NUM_REGS];
|
2010-07-27 04:53:44 +00:00
|
|
|
|
2011-11-14 18:38:19 +00:00
|
|
|
(void)arg;
|
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_getregs(regs) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("warning: device: can't fetch registers\n");
|
2010-07-27 04:53:44 +00:00
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
|
2010-08-13 00:25:28 +00:00
|
|
|
for (i = 0; i < device_default->max_breakpoints; i++) {
|
|
|
|
struct device_breakpoint *bp =
|
|
|
|
&device_default->breakpoints[i];
|
2010-07-27 04:53:44 +00:00
|
|
|
|
|
|
|
if ((bp->flags & DEVICE_BP_ENABLED) &&
|
2012-04-25 00:43:14 +00:00
|
|
|
bp->type == DEVICE_BPTYPE_BREAK &&
|
2010-07-27 04:53:44 +00:00
|
|
|
bp->addr == regs[0])
|
|
|
|
break;
|
|
|
|
}
|
2010-04-10 02:35:36 +00:00
|
|
|
|
2010-08-13 00:25:28 +00:00
|
|
|
if (i < device_default->max_breakpoints) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Stepping over breakpoint #%d at 0x%04x\n",
|
2010-07-27 04:53:44 +00:00
|
|
|
i, regs[0]);
|
2011-03-15 00:12:48 +00:00
|
|
|
device_ctl(DEVICE_CTL_STEP);
|
2010-07-27 04:53:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_ctl(DEVICE_CTL_RUN) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("run: failed to start CPU\n");
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
2010-07-27 04:53:44 +00:00
|
|
|
}
|
2010-04-10 02:35:36 +00:00
|
|
|
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Running. Press Ctrl+C to interrupt...\n");
|
2010-04-10 02:35:36 +00:00
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
do {
|
2011-03-15 00:12:48 +00:00
|
|
|
status = device_poll();
|
2010-04-30 04:01:03 +00:00
|
|
|
} while (status == DEVICE_STATUS_RUNNING);
|
|
|
|
|
2010-04-10 02:35:36 +00:00
|
|
|
if (status == DEVICE_STATUS_INTR)
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("\n");
|
2010-04-10 02:35:36 +00:00
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
if (status == DEVICE_STATUS_ERROR)
|
|
|
|
return -1;
|
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_ctl(DEVICE_CTL_HALT) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
return cmd_regs(NULL);
|
2010-04-10 02:35:36 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int cmd_set(char **arg)
|
2010-04-10 02:35:36 +00:00
|
|
|
{
|
|
|
|
char *reg_text = get_arg(arg);
|
|
|
|
char *val_text = get_arg(arg);
|
|
|
|
int reg;
|
2010-08-05 01:59:33 +00:00
|
|
|
address_t value = 0;
|
2010-08-05 02:43:07 +00:00
|
|
|
address_t regs[DEVICE_NUM_REGS];
|
2010-04-10 02:35:36 +00:00
|
|
|
|
|
|
|
if (!(reg_text && val_text)) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("set: must specify a register and a value\n");
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-05-22 04:17:06 +00:00
|
|
|
reg = dis_reg_from_name(reg_text);
|
|
|
|
if (reg < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("set: unknown register: %s\n", reg_text);
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-15 02:20:50 +00:00
|
|
|
if (expr_eval(val_text, &value) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("set: can't parse value: %s\n", val_text);
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_getregs(regs) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
regs[reg] = value;
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_setregs(regs) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-08-13 03:45:08 +00:00
|
|
|
show_regs(regs);
|
2010-04-10 02:35:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int cmd_dis(char **arg)
|
2010-04-10 02:35:36 +00:00
|
|
|
{
|
|
|
|
char *off_text = get_arg(arg);
|
|
|
|
char *len_text = get_arg(arg);
|
2010-08-05 01:59:33 +00:00
|
|
|
address_t offset = 0;
|
|
|
|
address_t length = 0x40;
|
2010-08-10 02:03:56 +00:00
|
|
|
uint8_t *buf;
|
2010-04-10 02:35:36 +00:00
|
|
|
|
|
|
|
if (!off_text) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("dis: offset must be specified\n");
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-15 02:20:50 +00:00
|
|
|
if (expr_eval(off_text, &offset) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("dis: can't parse offset: %s\n", off_text);
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len_text) {
|
2011-03-15 02:20:50 +00:00
|
|
|
if (expr_eval(len_text, &length) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("dis: can't parse length: %s\n",
|
2010-04-10 02:35:36 +00:00
|
|
|
len_text);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-06-27 06:54:08 +00:00
|
|
|
} else if (offset < 0x10000 && offset + length > 0x10000) {
|
2010-04-10 02:35:36 +00:00
|
|
|
length = 0x10000 - offset;
|
|
|
|
}
|
|
|
|
|
2010-08-10 02:03:56 +00:00
|
|
|
buf = malloc(length);
|
|
|
|
if (!buf) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("dis: couldn't allocate memory");
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
2010-08-10 02:03:56 +00:00
|
|
|
}
|
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_readmem(offset, buf, length) < 0) {
|
2010-08-10 02:03:56 +00:00
|
|
|
free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-04-10 02:35:36 +00:00
|
|
|
|
2010-10-11 23:48:25 +00:00
|
|
|
reader_set_repeat("dis 0x%x 0x%x", offset + length, length);
|
2012-10-03 23:10:36 +00:00
|
|
|
disassemble(offset, buf, length, device_default->power_buf);
|
2010-08-10 02:03:56 +00:00
|
|
|
free(buf);
|
2010-04-10 02:35:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-30 09:53:58 +00:00
|
|
|
struct hexout_data {
|
|
|
|
FILE *file;
|
2010-08-05 04:38:05 +00:00
|
|
|
address_t addr;
|
2010-05-27 01:58:00 +00:00
|
|
|
uint8_t buf[16];
|
2010-04-30 09:53:58 +00:00
|
|
|
int len;
|
2010-08-05 04:38:05 +00:00
|
|
|
|
|
|
|
uint16_t segoff;
|
2010-04-30 09:53:58 +00:00
|
|
|
};
|
2010-04-10 02:35:36 +00:00
|
|
|
|
2010-04-30 09:53:58 +00:00
|
|
|
static int hexout_start(struct hexout_data *hexout, const char *filename)
|
2010-04-10 02:35:36 +00:00
|
|
|
{
|
2011-07-28 15:20:44 +00:00
|
|
|
char * path = NULL;
|
|
|
|
|
|
|
|
path = expand_tilde(filename);
|
|
|
|
if (!path)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
hexout->file = fopen(path, "w");
|
|
|
|
free(path);
|
|
|
|
|
2010-04-30 09:53:58 +00:00
|
|
|
if (!hexout->file) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("hexout: couldn't open output file");
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-04-30 09:53:58 +00:00
|
|
|
hexout->addr = 0;
|
|
|
|
hexout->len = 0;
|
2010-08-05 04:38:05 +00:00
|
|
|
hexout->segoff = 0;
|
2010-04-30 09:53:58 +00:00
|
|
|
|
2010-04-10 02:35:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-14 18:38:19 +00:00
|
|
|
static int hexout_write(FILE *out, int len, uint16_t addr,
|
2010-08-05 04:38:05 +00:00
|
|
|
const uint8_t *payload)
|
2010-04-10 02:35:36 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int cksum = 0;
|
|
|
|
|
2010-08-05 04:38:05 +00:00
|
|
|
if (fprintf(out, ":%02X%04X00", len, addr) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
goto fail;
|
2010-08-05 04:38:05 +00:00
|
|
|
cksum += len;
|
|
|
|
cksum += addr & 0xff;
|
|
|
|
cksum += addr >> 8;
|
2010-04-10 02:35:36 +00:00
|
|
|
|
2010-08-05 04:38:05 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (fprintf(out, "%02X", payload[i]) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
goto fail;
|
2010-08-05 04:38:05 +00:00
|
|
|
cksum += payload[i];
|
2010-04-10 02:35:36 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 04:38:05 +00:00
|
|
|
if (fprintf(out, "%02X\n", ~(cksum - 1) & 0xff) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("hexout: can't write HEX data");
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-05 04:38:05 +00:00
|
|
|
static int hexout_flush(struct hexout_data *hexout)
|
|
|
|
{
|
|
|
|
address_t addr_low = hexout->addr & 0xffff;
|
|
|
|
address_t segoff = hexout->addr >> 16;
|
|
|
|
|
|
|
|
if (!hexout->len)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (segoff != hexout->segoff) {
|
|
|
|
uint8_t offset_data[] = {segoff >> 8, segoff & 0xff};
|
|
|
|
|
2011-11-14 18:38:19 +00:00
|
|
|
if (hexout_write(hexout->file, 2, 0, offset_data) < 0)
|
2010-08-05 04:38:05 +00:00
|
|
|
return -1;
|
|
|
|
hexout->segoff = segoff;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hexout_write(hexout->file, hexout->len, addr_low,
|
2011-11-14 18:38:19 +00:00
|
|
|
hexout->buf) < 0)
|
2010-08-05 04:38:05 +00:00
|
|
|
return -1;
|
|
|
|
hexout->len = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-30 09:53:58 +00:00
|
|
|
static int hexout_feed(struct hexout_data *hexout,
|
2010-05-13 00:57:21 +00:00
|
|
|
uint16_t addr, const uint8_t *buf, int len)
|
2010-04-10 02:35:36 +00:00
|
|
|
{
|
|
|
|
while (len) {
|
|
|
|
int count;
|
|
|
|
|
2010-04-30 09:53:58 +00:00
|
|
|
if ((hexout->addr + hexout->len != addr ||
|
|
|
|
hexout->len >= sizeof(hexout->buf)) &&
|
|
|
|
hexout_flush(hexout) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-04-30 09:53:58 +00:00
|
|
|
if (!hexout->len)
|
|
|
|
hexout->addr = addr;
|
2010-04-10 02:35:36 +00:00
|
|
|
|
2010-04-30 09:53:58 +00:00
|
|
|
count = sizeof(hexout->buf) - hexout->len;
|
2010-04-10 02:35:36 +00:00
|
|
|
if (count > len)
|
|
|
|
count = len;
|
|
|
|
|
2010-04-30 09:53:58 +00:00
|
|
|
memcpy(hexout->buf + hexout->len, buf, count);
|
|
|
|
hexout->len += count;
|
2010-04-10 02:35:36 +00:00
|
|
|
|
|
|
|
addr += count;
|
|
|
|
buf += count;
|
|
|
|
len -= count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int cmd_hexout(char **arg)
|
2010-04-10 02:35:36 +00:00
|
|
|
{
|
|
|
|
char *off_text = get_arg(arg);
|
|
|
|
char *len_text = get_arg(arg);
|
|
|
|
char *filename = *arg;
|
2010-08-05 01:59:33 +00:00
|
|
|
address_t off;
|
|
|
|
address_t length;
|
2010-04-30 09:53:58 +00:00
|
|
|
struct hexout_data hexout;
|
2010-04-10 02:35:36 +00:00
|
|
|
|
|
|
|
if (!(off_text && len_text && *filename)) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("hexout: need offset, length and filename\n");
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-15 02:20:50 +00:00
|
|
|
if (expr_eval(off_text, &off) < 0 ||
|
|
|
|
expr_eval(len_text, &length) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-04-30 09:53:58 +00:00
|
|
|
if (hexout_start(&hexout, filename) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (length) {
|
2011-02-20 22:30:32 +00:00
|
|
|
uint8_t buf[4096];
|
2010-04-10 02:35:36 +00:00
|
|
|
int count = length;
|
|
|
|
|
|
|
|
if (count > sizeof(buf))
|
|
|
|
count = sizeof(buf);
|
|
|
|
|
2011-03-11 01:59:02 +00:00
|
|
|
printc("Reading %4d bytes from 0x%04x...\n", count, off);
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_readmem(off, buf, count) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("hexout: can't read memory");
|
2010-04-10 02:35:36 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2010-04-30 09:53:58 +00:00
|
|
|
if (hexout_feed(&hexout, off, buf, count) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
length -= count;
|
|
|
|
off += count;
|
|
|
|
}
|
|
|
|
|
2010-04-30 09:53:58 +00:00
|
|
|
if (hexout_flush(&hexout) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
goto fail;
|
2013-01-31 02:35:57 +00:00
|
|
|
|
|
|
|
if (fprintf(hexout.file, ":00000001FF\n") < 0) {
|
|
|
|
pr_error("hexout: failed to write terminator\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2010-04-30 09:53:58 +00:00
|
|
|
if (fclose(hexout.file) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("hexout: error on close");
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
2010-04-30 09:53:58 +00:00
|
|
|
fclose(hexout.file);
|
2010-04-10 02:35:36 +00:00
|
|
|
unlink(filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-10-12 00:52:56 +00:00
|
|
|
static int cmd_prog_feed(void *user_data, const struct binfile_chunk *ch)
|
2010-04-10 02:35:36 +00:00
|
|
|
{
|
2011-10-12 00:52:56 +00:00
|
|
|
return prog_feed((struct prog_data *)user_data, ch);
|
2010-04-10 02:35:36 +00:00
|
|
|
}
|
|
|
|
|
2010-09-12 23:27:11 +00:00
|
|
|
static int do_cmd_prog(char **arg, int prog_flags)
|
2010-04-10 02:35:36 +00:00
|
|
|
{
|
|
|
|
FILE *in;
|
2010-04-30 04:01:03 +00:00
|
|
|
struct prog_data prog;
|
2011-07-28 15:20:44 +00:00
|
|
|
char * path;
|
2010-04-10 02:35:36 +00:00
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
if (prompt_abort(MODIFY_SYMS))
|
2010-04-10 02:35:36 +00:00
|
|
|
return 0;
|
|
|
|
|
2011-07-28 15:20:44 +00:00
|
|
|
path = expand_tilde(*arg);
|
|
|
|
if (!path)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
in = fopen(path, "rb");
|
|
|
|
free(path);
|
2010-04-10 02:35:36 +00:00
|
|
|
if (!in) {
|
2011-07-26 13:51:06 +00:00
|
|
|
printc_err("prog: %s: %s\n", *arg, last_error());
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_ctl(DEVICE_CTL_HALT) < 0) {
|
2010-04-10 02:35:36 +00:00
|
|
|
fclose(in);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-12 23:27:11 +00:00
|
|
|
prog_init(&prog, prog_flags);
|
2010-04-10 02:35:36 +00:00
|
|
|
|
2010-09-10 00:52:47 +00:00
|
|
|
if (binfile_extract(in, cmd_prog_feed, &prog) < 0) {
|
2010-05-15 03:13:58 +00:00
|
|
|
fclose(in);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-07-18 02:03:55 +00:00
|
|
|
if ((prog_flags & PROG_WANT_ERASE) &&
|
|
|
|
(binfile_info(in) & BINFILE_HAS_SYMS)) {
|
2011-03-15 02:20:50 +00:00
|
|
|
stab_clear();
|
|
|
|
binfile_syms(in);
|
2010-04-10 02:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(in);
|
|
|
|
|
2010-04-30 04:01:03 +00:00
|
|
|
if (prog_flush(&prog) < 0)
|
2010-04-10 02:35:36 +00:00
|
|
|
return -1;
|
|
|
|
|
2012-07-18 02:03:55 +00:00
|
|
|
printc("Done, %d bytes total\n", prog.total_written);
|
2011-12-26 23:58:28 +00:00
|
|
|
|
|
|
|
if (device_ctl(DEVICE_CTL_RESET) < 0)
|
|
|
|
printc_err("warning: prog: "
|
|
|
|
"failed to reset after programming\n");
|
2010-04-10 02:35:36 +00:00
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
unmark_modified(MODIFY_SYMS);
|
2010-05-15 03:13:58 +00:00
|
|
|
return 0;
|
2010-04-10 02:35:36 +00:00
|
|
|
}
|
|
|
|
|
2010-09-12 23:27:11 +00:00
|
|
|
int cmd_prog(char **arg)
|
|
|
|
{
|
|
|
|
return do_cmd_prog(arg, PROG_WANT_ERASE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_load(char **arg)
|
|
|
|
{
|
|
|
|
return do_cmd_prog(arg, 0);
|
|
|
|
}
|
|
|
|
|
2012-07-18 02:03:55 +00:00
|
|
|
int cmd_verify(char **arg)
|
|
|
|
{
|
|
|
|
return do_cmd_prog(arg, PROG_VERIFY);
|
|
|
|
}
|
|
|
|
|
2012-04-25 00:43:14 +00:00
|
|
|
static int do_setbreak(device_bptype_t type, char **arg)
|
2010-07-02 02:22:52 +00:00
|
|
|
{
|
|
|
|
char *addr_text = get_arg(arg);
|
|
|
|
char *index_text = get_arg(arg);
|
2010-07-27 01:08:21 +00:00
|
|
|
int index = -1;
|
2010-08-05 01:59:33 +00:00
|
|
|
address_t addr;
|
2010-07-02 02:22:52 +00:00
|
|
|
|
|
|
|
if (!addr_text) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("setbreak: address required\n");
|
2010-07-02 02:22:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-03-15 02:20:50 +00:00
|
|
|
if (expr_eval(addr_text, &addr) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("setbreak: invalid address\n");
|
2010-07-02 02:22:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index_text) {
|
2010-10-11 22:26:36 +00:00
|
|
|
address_t val;
|
2010-07-02 02:22:52 +00:00
|
|
|
|
2011-03-15 02:20:50 +00:00
|
|
|
if (expr_eval(index_text, &val) < 0 ||
|
2010-10-11 22:26:36 +00:00
|
|
|
val >= device_default->max_breakpoints) {
|
2012-04-25 00:43:14 +00:00
|
|
|
printc("setbreak: invalid breakpoint slot: %d\n",
|
|
|
|
val);
|
2010-07-02 02:22:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-10-11 22:26:36 +00:00
|
|
|
|
|
|
|
index = val;
|
2010-07-02 02:22:52 +00:00
|
|
|
}
|
|
|
|
|
2012-04-25 00:43:14 +00:00
|
|
|
index = device_setbrk(device_default, index, 1, addr, type);
|
2010-07-27 01:08:21 +00:00
|
|
|
if (index < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("setbreak: all breakpoint slots are "
|
2010-07-27 01:08:21 +00:00
|
|
|
"occupied\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-07-02 02:22:52 +00:00
|
|
|
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Set breakpoint %d\n", index);
|
2010-07-02 02:22:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-25 00:43:14 +00:00
|
|
|
int cmd_setbreak(char **arg)
|
|
|
|
{
|
|
|
|
return do_setbreak(DEVICE_BPTYPE_BREAK, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_setwatch(char **arg)
|
|
|
|
{
|
|
|
|
return do_setbreak(DEVICE_BPTYPE_WATCH, arg);
|
|
|
|
}
|
|
|
|
|
2012-04-25 02:10:26 +00:00
|
|
|
int cmd_setwatch_w(char **arg)
|
|
|
|
{
|
|
|
|
return do_setbreak(DEVICE_BPTYPE_WRITE, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_setwatch_r(char **arg)
|
|
|
|
{
|
|
|
|
return do_setbreak(DEVICE_BPTYPE_READ, arg);
|
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int cmd_delbreak(char **arg)
|
2010-07-02 02:22:52 +00:00
|
|
|
{
|
|
|
|
char *index_text = get_arg(arg);
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (index_text) {
|
2010-10-11 22:26:36 +00:00
|
|
|
address_t index;
|
2010-07-02 02:22:52 +00:00
|
|
|
|
2011-03-15 02:20:50 +00:00
|
|
|
if (expr_eval(index_text, &index) < 0 ||
|
2010-10-11 22:26:36 +00:00
|
|
|
index >= device_default->max_breakpoints) {
|
|
|
|
printc("delbreak: invalid breakpoint slot: %d\n",
|
|
|
|
index);
|
2010-07-27 01:08:21 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Clearing breakpoint %d\n", index);
|
2012-04-25 00:43:14 +00:00
|
|
|
device_setbrk(device_default, index, 0, 0, 0);
|
2010-07-02 02:22:52 +00:00
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("Clearing all breakpoints...\n");
|
2010-08-13 00:25:28 +00:00
|
|
|
for (i = 0; i < device_default->max_breakpoints; i++)
|
2012-04-25 00:43:14 +00:00
|
|
|
device_setbrk(device_default, i, 0, 0, 0);
|
2010-07-02 02:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int cmd_break(char **arg)
|
2010-07-02 02:22:52 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-11-14 18:38:19 +00:00
|
|
|
(void)arg;
|
|
|
|
|
2012-04-25 00:43:14 +00:00
|
|
|
printc("%d breakpoints available:\n",
|
|
|
|
device_default->max_breakpoints);
|
2010-08-13 00:25:28 +00:00
|
|
|
for (i = 0; i < device_default->max_breakpoints; i++) {
|
|
|
|
const struct device_breakpoint *bp =
|
|
|
|
&device_default->breakpoints[i];
|
2010-07-02 02:22:52 +00:00
|
|
|
|
2010-07-27 01:08:21 +00:00
|
|
|
if (bp->flags & DEVICE_BP_ENABLED) {
|
2010-07-02 02:22:52 +00:00
|
|
|
char name[128];
|
2011-03-15 03:00:34 +00:00
|
|
|
|
2013-01-31 02:31:42 +00:00
|
|
|
print_address(bp->addr, name, sizeof(name), 0);
|
2012-04-25 00:43:14 +00:00
|
|
|
printc(" %d. %s", i, name);
|
|
|
|
|
2012-04-25 02:10:26 +00:00
|
|
|
switch (bp->type) {
|
|
|
|
case DEVICE_BPTYPE_WATCH:
|
2012-04-25 00:43:14 +00:00
|
|
|
printc(" [watchpoint]\n");
|
2012-04-25 02:10:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DEVICE_BPTYPE_READ:
|
|
|
|
printc(" [read watchpoint]\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DEVICE_BPTYPE_WRITE:
|
|
|
|
printc(" [write watchpoint]\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DEVICE_BPTYPE_BREAK:
|
2012-04-25 00:43:14 +00:00
|
|
|
printc("\n");
|
2012-04-25 02:10:26 +00:00
|
|
|
break;
|
2012-04-25 00:43:14 +00:00
|
|
|
}
|
2010-07-02 02:22:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2010-09-23 07:05:39 +00:00
|
|
|
|
|
|
|
#define FCTL3 0x012c
|
|
|
|
#define FCTL3_LOCKA 0x40
|
|
|
|
#define FWKEY 0xa5
|
|
|
|
#define FRKEY 0x96
|
|
|
|
|
|
|
|
int cmd_locka(char **arg)
|
|
|
|
{
|
|
|
|
const char *status = get_arg(arg);
|
|
|
|
int value = 0;
|
|
|
|
uint8_t regval[2];
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
if (!strcasecmp(status, "set")) {
|
|
|
|
value = FCTL3_LOCKA;
|
|
|
|
} else if (!strcasecmp(status, "clear")) {
|
|
|
|
value = 0;
|
|
|
|
} else {
|
|
|
|
printc_err("locka: unknown action: %s\n", status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_readmem(FCTL3, regval, 2) < 0) {
|
2010-09-23 07:05:39 +00:00
|
|
|
printc_err("locka: can't read FCTL3 register\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (regval[1] != FRKEY) {
|
|
|
|
printc_err("warning: locka: read key invalid (got 0x%02x)\n",
|
|
|
|
regval[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status && ((regval[0] & FCTL3_LOCKA) != value)) {
|
|
|
|
printc_dbg("Toggling LOCKA bit\n");
|
|
|
|
|
|
|
|
regval[0] |= FCTL3_LOCKA;
|
|
|
|
regval[1] = FWKEY;
|
2010-09-23 07:34:12 +00:00
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_writemem(FCTL3, regval, 2) < 0) {
|
2010-09-23 07:05:39 +00:00
|
|
|
printc_err("locka: can't write FCTL3 register\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-23 07:34:12 +00:00
|
|
|
|
2011-03-15 00:12:48 +00:00
|
|
|
if (device_readmem(FCTL3, regval, 2) < 0) {
|
2010-09-23 07:34:12 +00:00
|
|
|
printc_err("locka: can't read FCTL3 register\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-23 07:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printc("LOCKA is %s\n", (regval[0] & FCTL3_LOCKA) ? "set" : "clear");
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-09 04:07:05 +00:00
|
|
|
|
|
|
|
int cmd_fill(char **arg)
|
|
|
|
{
|
|
|
|
char *addr_text = get_arg(arg);
|
|
|
|
char *len_text = get_arg(arg);
|
|
|
|
char *byte_text;
|
|
|
|
address_t addr = 0;
|
|
|
|
address_t len = 0;
|
|
|
|
uint8_t buf[256];
|
|
|
|
int period = 0;
|
|
|
|
int phase = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!(addr_text && len_text)) {
|
|
|
|
printc_err("fill: address and length must be supplied\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expr_eval(addr_text, &addr) < 0) {
|
|
|
|
printc_err("fill: invalid address\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expr_eval(len_text, &len) < 0) {
|
|
|
|
printc_err("fill: invalid length\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((byte_text = get_arg(arg))) {
|
|
|
|
if (period >= sizeof(buf)) {
|
|
|
|
printc_err("fill: maximum length exceeded\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[period++] = strtoul(byte_text, NULL, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!period) {
|
|
|
|
printc_err("fill: no pattern supplied\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = period; i < sizeof(buf); i++)
|
|
|
|
buf[i] = buf[i % period];
|
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
int plen = sizeof(buf) - phase;
|
|
|
|
|
|
|
|
if (plen > len)
|
|
|
|
plen = len;
|
|
|
|
|
|
|
|
if (device_writemem(addr, buf + phase, plen) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
addr += plen;
|
|
|
|
len -= plen;
|
|
|
|
phase = (phase + plen) % period;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|