All output now goes through buffering/filtering.

The following substitutions have been made:

    printf -> printc
    fprintf(stderr, ...) -> printc_err(...)
    perror -> pr_error
This commit is contained in:
Daniel Beer 2010-08-17 11:07:03 +12:00
parent b6eda4e225
commit 87f89adc44
26 changed files with 430 additions and 402 deletions

View File

@ -24,6 +24,7 @@
#include "titext.h"
#include "srec.h"
#include "coff.h"
#include "output.h"
struct file_format {
int (*check)(FILE *in);
@ -94,12 +95,12 @@ int binfile_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
const struct file_format *fmt = identify(in);
if (!fmt) {
fprintf(stderr, "binfile: unknown file format\n");
printc_err("binfile: unknown file format\n");
return -1;
}
if (!fmt->extract) {
fprintf(stderr, "binfile: this format contains no code\n");
printc_err("binfile: this format contains no code\n");
return -1;
}
@ -111,12 +112,12 @@ int binfile_syms(FILE *in, stab_t stab)
const struct file_format *fmt = identify(in);
if (!fmt) {
fprintf(stderr, "binfile: unknown file format\n");
printc_err("binfile: unknown file format\n");
return -1;
}
if (!fmt->syms) {
fprintf(stderr, "binfile: this format contains no symbols\n");
printc_err("binfile: this format contains no symbols\n");
return -1;
}

53
bsl.c
View File

@ -28,6 +28,7 @@
#include "bsl.h"
#include "util.h"
#include "output.h"
#include "fet_error.h"
#ifdef __APPLE__
@ -51,17 +52,17 @@ static int bsl_ack(struct bsl_device *dev)
uint8_t reply;
if (read_with_timeout(dev->serial_fd, &reply, 1) < 0) {
fprintf(stderr, "bsl: failed to receive reply\n");
printc_err("bsl: failed to receive reply\n");
return -1;
}
if (reply == DATA_NAK) {
fprintf(stderr, "bsl: received NAK\n");
printc_err("bsl: received NAK\n");
return -1;
}
if (reply != DATA_ACK) {
fprintf(stderr, "bsl: bad ack character: %x\n", reply);
printc_err("bsl: bad ack character: %x\n", reply);
return -1;
}
@ -74,7 +75,7 @@ static int bsl_sync(struct bsl_device *dev)
int tries = 2;
if (tcflush(dev->serial_fd, TCIFLUSH) < 0) {
perror("bsl: tcflush");
pr_error("bsl: tcflush");
return -1;
}
@ -82,7 +83,7 @@ static int bsl_sync(struct bsl_device *dev)
if (!(write_all(dev->serial_fd, &c, 1) || bsl_ack(dev)))
return 0;
fprintf(stderr, "bsl: sync failed\n");
printc_err("bsl: sync failed\n");
return -1;
}
@ -97,7 +98,7 @@ static int send_command(struct bsl_device *dev,
int i;
if (pktlen + 6 > sizeof(pktbuf)) {
fprintf(stderr, "bsl: payload too large: %d\n", len);
printc_err("bsl: payload too large: %d\n", len);
return -1;
}
@ -136,7 +137,7 @@ static int verify_checksum(struct bsl_device *dev)
ckhigh ^= dev->reply_buf[i];
if (cklow || ckhigh) {
fprintf(stderr, "bsl: checksum invalid (%02x %02x)\n",
printc_err("bsl: checksum invalid (%02x %02x)\n",
cklow, ckhigh);
return -1;
}
@ -166,16 +167,16 @@ static int fetch_reply(struct bsl_device *dev)
dev->reply_len == dev->reply_buf[2] + 6)
return verify_checksum(dev);
} else if (dev->reply_buf[0] == DATA_NAK) {
fprintf(stderr, "bsl: received NAK\n");
printc_err("bsl: received NAK\n");
return -1;
} else {
fprintf(stderr, "bsl: unknown reply type: %02x\n",
printc_err("bsl: unknown reply type: %02x\n",
dev->reply_buf[0]);
return -1;
}
if (dev->reply_len >= sizeof(dev->reply_buf)) {
fprintf(stderr, "bsl: reply buffer overflow\n");
printc_err("bsl: reply buffer overflow\n");
return -1;
}
}
@ -188,7 +189,7 @@ static int bsl_xfer(struct bsl_device *dev,
if (bsl_sync(dev) < 0 ||
send_command(dev, command_code, addr, txdata, len) < 0 ||
fetch_reply(dev) < 0) {
fprintf(stderr, "bsl: failed on command 0x%02x "
printc_err("bsl: failed on command 0x%02x "
"(addr = 0x%04x, len = 0x%04x)\n",
command_code, addr, len);
return -1;
@ -234,7 +235,7 @@ static int bsl_ctl(device_t dev_base, device_ctl_t type)
return 0;
default:
fprintf(stderr, "bsl: CPU control is not possible\n");
printc_err("bsl: CPU control is not possible\n");
}
return -1;
@ -247,13 +248,13 @@ static device_status_t bsl_poll(device_t dev_base)
static int bsl_getregs(device_t dev_base, address_t *regs)
{
fprintf(stderr, "bsl: register fetch is not implemented\n");
printc_err("bsl: register fetch is not implemented\n");
return -1;
}
static int bsl_setregs(device_t dev_base, const address_t *regs)
{
fprintf(stderr, "bsl: register store is not implemented\n");
printc_err("bsl: register store is not implemented\n");
return -1;
}
@ -269,7 +270,7 @@ static int bsl_writemem(device_t dev_base,
r = bsl_xfer(dev, CMD_RX_DATA, addr, mem, wlen);
if( r < 0 ) {
fprintf(stderr, "bsl: failed to write to 0x%04x\n",
printc_err("bsl: failed to write to 0x%04x\n",
addr);
return -1;
}
@ -288,7 +289,7 @@ static int bsl_readmem(device_t dev_base,
struct bsl_device *dev = (struct bsl_device *)dev_base;
if ((addr | len | (addr + len)) & 0xffff0000) {
fprintf(stderr, "bsl: memory read out of range\n");
printc_err("bsl: memory read out of range\n");
return -1;
}
@ -299,7 +300,7 @@ static int bsl_readmem(device_t dev_base,
count = 128;
if (bsl_xfer(dev, CMD_TX_DATA, addr, NULL, count) < 0) {
fprintf(stderr, "bsl: failed to read memory\n");
printc_err("bsl: failed to read memory\n");
return -1;
}
@ -323,7 +324,7 @@ static int enter_via_fet(struct bsl_device *dev)
/* Enter bootloader command */
if (write_all(dev->serial_fd,
(uint8_t *)"\x7e\x24\x01\x9d\x5a\x7e", 6)) {
fprintf(stderr, "bsl: couldn't write bootloader transition "
printc_err("bsl: couldn't write bootloader transition "
"command\n");
return -1;
}
@ -333,7 +334,7 @@ static int enter_via_fet(struct bsl_device *dev)
int r = read_with_timeout(dev->serial_fd, data, len);
if (r < 0) {
fprintf(stderr, "bsl: couldn't read bootloader "
printc_err("bsl: couldn't read bootloader "
"transition acknowledgement\n");
return -1;
}
@ -344,7 +345,7 @@ static int enter_via_fet(struct bsl_device *dev)
/* Check that it's what we expect */
if (memcmp(buf, "\x06\x00\x24\x00\x00\x00\x61\x01", 8)) {
fprintf(stderr, "bsl: bootloader start returned error "
printc_err("bsl: bootloader start returned error "
"%d (%s)\n", buf[5], fet_error(buf[5]));
return -1;
}
@ -357,7 +358,7 @@ device_t bsl_open(const char *device)
struct bsl_device *dev = malloc(sizeof(*dev));
if (!dev) {
perror("bsl: can't allocate memory");
pr_error("bsl: can't allocate memory");
return NULL;
}
@ -373,7 +374,7 @@ device_t bsl_open(const char *device)
dev->serial_fd = open_serial(device, B460800);
if (dev->serial_fd < 0) {
fprintf(stderr, "bsl: can't open %s: %s\n",
printc_err("bsl: can't open %s: %s\n",
device, strerror(errno));
free(dev);
return NULL;
@ -386,18 +387,18 @@ device_t bsl_open(const char *device)
/* Show chip info */
if (bsl_xfer(dev, CMD_TX_DATA, 0xff0, NULL, 0x10) < 0) {
fprintf(stderr, "bsl: failed to read chip info\n");
printc_err("bsl: failed to read chip info\n");
goto fail;
}
if (dev->reply_len < 0x16) {
fprintf(stderr, "bsl: missing chip info\n");
printc_err("bsl: missing chip info\n");
goto fail;
}
printf("Device ID: 0x%02x%02x\n",
printc("Device ID: 0x%02x%02x\n",
dev->reply_buf[4], dev->reply_buf[5]);
printf("BSL version is %x.%02x\n", dev->reply_buf[14],
printc("BSL version is %x.%02x\n", dev->reply_buf[14],
dev->reply_buf[15]);
return (device_t)dev;

15
btree.c
View File

@ -22,6 +22,7 @@
#include <string.h>
#include <errno.h>
#include "btree.h"
#include "output.h"
#define MAX_HEIGHT 16
@ -152,8 +153,8 @@ static struct btree_page *allocate_page(btree_t bt, int height)
p = malloc(size);
if (!p) {
fprintf(stderr, "btree: couldn't allocate page: %s\n",
strerror(errno));
printc_err("btree: couldn't allocate page: %s\n",
strerror(errno));
return NULL;
}
@ -427,14 +428,14 @@ btree_t btree_alloc(const struct btree_def *def)
btree_t bt;
if (def->branches < 2 || (def->branches & 1)) {
fprintf(stderr, "btree: invalid branch count: %d\n",
printc_err("btree: invalid branch count: %d\n",
def->branches);
return NULL;
}
bt = malloc(sizeof(*bt));
if (!bt) {
fprintf(stderr, "btree: couldn't allocate tree: %s\n",
printc_err("btree: couldn't allocate tree: %s\n",
strerror(errno));
return NULL;
}
@ -445,7 +446,7 @@ btree_t btree_alloc(const struct btree_def *def)
bt->root = allocate_page(bt, 0);
if (!bt->root) {
fprintf(stderr, "btree: couldn't allocate root node: %s\n",
printc_err("btree: couldn't allocate root node: %s\n",
strerror(errno));
free(bt);
return NULL;
@ -505,7 +506,7 @@ int btree_put(btree_t bt, const void *key, const void *data)
/* Special case: cursor overwrite */
if (!key) {
if (bt->slot[0] < 0) {
fprintf(stderr, "btree: put at invalid cursor\n");
printc_err("btree: put at invalid cursor\n");
return -1;
}
@ -542,7 +543,7 @@ int btree_put(btree_t bt, const void *key, const void *data)
*/
if (h > bt->root->height) {
if (h >= MAX_HEIGHT) {
fprintf(stderr, "btree: maximum height exceeded\n");
printc_err("btree: maximum height exceeded\n");
goto fail;
}

35
coff.c
View File

@ -21,6 +21,7 @@
#include <errno.h>
#include "coff.h"
#include "util.h"
#include "output.h"
struct coff_header {
uint16_t version;
@ -101,26 +102,26 @@ static int read_block(FILE *in, int offset, int size, void *buf)
int len;
if (size < 0) {
fprintf(stderr, "coff: invalid size: %d\n", size);
printc_err("coff: invalid size: %d\n", size);
return -1;
}
if (fseek(in, offset, SEEK_SET) < 0) {
fprintf(stderr, "coff: can't seek to offset %d: %s\n",
printc_err("coff: can't seek to offset %d: %s\n",
offset, strerror(errno));
return -1;
}
len = fread(buf, 1, size, in);
if (len < 0) {
fprintf(stderr, "coff: can't read %d bytes from "
printc_err("coff: can't read %d bytes from "
"offset %d: %s\n",
size, offset, strerror(errno));
return -1;
}
if (len < size) {
fprintf(stderr, "coff: can't read %d bytes from "
printc_err("coff: can't read %d bytes from "
"offset %d: short read\n",
size, offset);
return -1;
@ -146,7 +147,7 @@ static int read_header(FILE *in, struct coff_header *hdr)
uint8_t hdr_data[FILE_HEADER_SIZE];
if (read_block(in, 0, FILE_HEADER_SIZE, hdr_data) < 0) {
fprintf(stderr, "coff: failed to extract COFF header\n");
printc_err("coff: failed to extract COFF header\n");
return -1;
}
@ -178,13 +179,13 @@ static int read_sechdrs(FILE *in, const struct coff_header *hdr,
table = malloc(alloc_size);
if (!table) {
perror("coff: can't allocate memory for section headers");
pr_error("coff: can't allocate memory for section headers");
return -1;
}
if (read_block(in, hdr->opt_bytes + FILE_HEADER_SIZE,
SHDR_SIZE * hdr->sec_count, table) < 0) {
fprintf(stderr, "coff: can't read section headers\n");
printc_err("coff: can't read section headers\n");
free(table);
return -1;
}
@ -204,13 +205,13 @@ static int load_section(FILE *in, uint32_t addr, uint32_t offset,
section = malloc(size);
if (!section) {
fprintf(stderr, "coff: couldn't allocate memory for "
printc_err("coff: couldn't allocate memory for "
"section at 0x%x: %s\n", offset, strerror(errno));
return -1;
}
if (read_block(in, offset, size, section) < 0) {
fprintf(stderr, "coff: couldn't read section at 0x%x\n",
printc_err("coff: couldn't read section at 0x%x\n",
offset);
free(section);
return -1;
@ -250,7 +251,7 @@ int coff_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
if (load_section(in, addr, offset, size,
cb, user_data) < 0) {
fprintf(stderr, "coff: error while loading "
printc_err("coff: error while loading "
"section %d\n", i);
ret = -1;
break;
@ -274,7 +275,7 @@ static int read_strtab(FILE *in, const struct coff_header *hdr,
int strtab_start = hdr->stab_count * STAB_ENTRY_SIZE + hdr->stab_start;
if (fseek(in, 0, SEEK_END) < 0) {
fprintf(stderr, "coff: can't seek to end\n");
printc_err("coff: can't seek to end\n");
return -1;
}
@ -282,7 +283,7 @@ static int read_strtab(FILE *in, const struct coff_header *hdr,
strtab_len = file_size - strtab_start;
if (strtab_len < 0) {
fprintf(stderr, "coff: invalid string table size\n");
printc_err("coff: invalid string table size\n");
return -1;
}
@ -294,12 +295,12 @@ static int read_strtab(FILE *in, const struct coff_header *hdr,
alloc_size = strtab_len + 1;
strtab = malloc(alloc_size);
if (!strtab) {
perror("coff: can't allocate memory for string table");
pr_error("coff: can't allocate memory for string table");
return -1;
}
if (read_block(in, strtab_start, strtab_len, strtab) < 0) {
fprintf(stderr, "coff: failed to read string table\n");
printc_err("coff: failed to read string table\n");
free(strtab);
return -1;
}
@ -322,13 +323,13 @@ static int read_symtab(FILE *in, const struct coff_header *hdr,
table = malloc(alloc_size);
if (!table) {
perror("coff: failed to allocate memory for symbol table");
pr_error("coff: failed to allocate memory for symbol table");
return -1;
}
if (read_block(in, hdr->stab_start,
STAB_ENTRY_SIZE * hdr->stab_count, table) < 0) {
fprintf(stderr, "coff: failed to read symbol table\n");
printc_err("coff: failed to read symbol table\n");
free(table);
return -1;
}
@ -379,7 +380,7 @@ int coff_syms(FILE *in, stab_t stab)
if ((storage_class == C_EXT || storage_class == C_LABEL) &&
stab_set(stab, name, value) < 0) {
fprintf(stderr, "coff: failed to insert symbol\n");
printc_err("coff: failed to insert symbol\n");
ret = -1;
break;
}

View File

@ -59,18 +59,18 @@ int cmd_md(char **arg)
address_t length = 0x40;
if (!off_text) {
fprintf(stderr, "md: offset must be specified\n");
printc_err("md: offset must be specified\n");
return -1;
}
if (expr_eval(stab_default, off_text, &offset) < 0) {
fprintf(stderr, "md: can't parse offset: %s\n", off_text);
printc_err("md: can't parse offset: %s\n", off_text);
return -1;
}
if (len_text) {
if (expr_eval(stab_default, len_text, &length) < 0) {
fprintf(stderr, "md: can't parse length: %s\n",
printc_err("md: can't parse length: %s\n",
len_text);
return -1;
}
@ -103,18 +103,18 @@ int cmd_mw(char **arg)
uint8_t buf[1024];
if (!off_text) {
fprintf(stderr, "md: offset must be specified\n");
printc_err("md: offset must be specified\n");
return -1;
}
if (expr_eval(stab_default, off_text, &offset) < 0) {
fprintf(stderr, "md: can't parse offset: %s\n", off_text);
printc_err("md: can't parse offset: %s\n", off_text);
return -1;
}
while ((byte_text = get_arg(arg))) {
if (length >= sizeof(buf)) {
fprintf(stderr, "md: maximum length exceeded\n");
printc_err("md: maximum length exceeded\n");
return -1;
}
@ -140,7 +140,7 @@ int cmd_erase(char **arg)
if (device_default->ctl(device_default, DEVICE_CTL_HALT) < 0)
return -1;
printf("Erasing...\n");
printc("Erasing...\n");
return device_default->ctl(device_default, DEVICE_CTL_ERASE);
}
@ -167,7 +167,7 @@ int cmd_run(char **arg)
address_t regs[DEVICE_NUM_REGS];
if (device_default->getregs(device_default, regs) < 0) {
fprintf(stderr, "warning: device: can't fetch registers\n");
printc_err("warning: device: can't fetch registers\n");
} else {
int i;
@ -181,25 +181,25 @@ int cmd_run(char **arg)
}
if (i < device_default->max_breakpoints) {
printf("Stepping over breakpoint #%d at 0x%04x\n",
printc("Stepping over breakpoint #%d at 0x%04x\n",
i, regs[0]);
device_default->ctl(device_default, DEVICE_CTL_STEP);
}
}
if (device_default->ctl(device_default, DEVICE_CTL_RUN) < 0) {
fprintf(stderr, "run: failed to start CPU\n");
printc_err("run: failed to start CPU\n");
return -1;
}
printf("Running. Press Ctrl+C to interrupt...\n");
printc("Running. Press Ctrl+C to interrupt...\n");
do {
status = device_default->poll(device_default);
} while (status == DEVICE_STATUS_RUNNING);
if (status == DEVICE_STATUS_INTR)
printf("\n");
printc("\n");
if (status == DEVICE_STATUS_ERROR)
return -1;
@ -219,18 +219,18 @@ int cmd_set(char **arg)
address_t regs[DEVICE_NUM_REGS];
if (!(reg_text && val_text)) {
fprintf(stderr, "set: must specify a register and a value\n");
printc_err("set: must specify a register and a value\n");
return -1;
}
reg = dis_reg_from_name(reg_text);
if (reg < 0) {
fprintf(stderr, "set: unknown register: %s\n", reg_text);
printc_err("set: unknown register: %s\n", reg_text);
return -1;
}
if (expr_eval(stab_default, val_text, &value) < 0) {
fprintf(stderr, "set: can't parse value: %s\n", val_text);
printc_err("set: can't parse value: %s\n", val_text);
return -1;
}
@ -253,18 +253,18 @@ int cmd_dis(char **arg)
uint8_t *buf;
if (!off_text) {
fprintf(stderr, "dis: offset must be specified\n");
printc_err("dis: offset must be specified\n");
return -1;
}
if (expr_eval(stab_default, off_text, &offset) < 0) {
fprintf(stderr, "dis: can't parse offset: %s\n", off_text);
printc_err("dis: can't parse offset: %s\n", off_text);
return -1;
}
if (len_text) {
if (expr_eval(stab_default, len_text, &length) < 0) {
fprintf(stderr, "dis: can't parse length: %s\n",
printc_err("dis: can't parse length: %s\n",
len_text);
return -1;
}
@ -274,7 +274,7 @@ int cmd_dis(char **arg)
buf = malloc(length);
if (!buf) {
perror("dis: couldn't allocate memory");
pr_error("dis: couldn't allocate memory");
return -1;
}
@ -302,7 +302,7 @@ static int hexout_start(struct hexout_data *hexout, const char *filename)
{
hexout->file = fopen(filename, "w");
if (!hexout->file) {
perror("hexout: couldn't open output file");
pr_error("hexout: couldn't open output file");
return -1;
}
@ -337,7 +337,7 @@ static int hexout_write(FILE *out, int len, uint16_t addr, int type,
return 0;
fail:
perror("hexout: can't write HEX data");
pr_error("hexout: can't write HEX data");
return -1;
}
@ -403,7 +403,7 @@ int cmd_hexout(char **arg)
struct hexout_data hexout;
if (!(off_text && len_text && *filename)) {
fprintf(stderr, "hexout: need offset, length and filename\n");
printc_err("hexout: need offset, length and filename\n");
return -1;
}
@ -421,10 +421,10 @@ int cmd_hexout(char **arg)
if (count > sizeof(buf))
count = sizeof(buf);
printf("Reading %d bytes from 0x%04x...\n", count, off);
printc("Reading %d bytes from 0x%04x...\n", count, off);
if (device_default->readmem(device_default,
off, buf, count) < 0) {
perror("hexout: can't read memory");
pr_error("hexout: can't read memory");
goto fail;
}
@ -438,7 +438,7 @@ int cmd_hexout(char **arg)
if (hexout_flush(&hexout) < 0)
goto fail;
if (fclose(hexout.file) < 0) {
perror("hexout: error on close");
pr_error("hexout: error on close");
return -1;
}
@ -467,14 +467,14 @@ static int prog_flush(struct prog_data *prog)
wlen = 0x999a - prog->addr;
if (!prog->have_erased) {
printf("Erasing...\n");
printc("Erasing...\n");
if (device_default->ctl(device_default,
DEVICE_CTL_ERASE) < 0)
return -1;
prog->have_erased = 1;
}
printf("Writing %3d bytes to %04x...\n", wlen, prog->addr);
printc("Writing %3d bytes to %04x...\n", wlen, prog->addr);
if (device_default->writemem(device_default, prog->addr,
prog->buf, wlen) < 0)
return -1;
@ -533,7 +533,7 @@ int cmd_prog(char **arg)
in = fopen(*arg, "r");
if (!in) {
fprintf(stderr, "prog: %s: %s\n", *arg, strerror(errno));
printc_err("prog: %s: %s\n", *arg, strerror(errno));
return -1;
}
@ -560,7 +560,7 @@ int cmd_prog(char **arg)
return -1;
if (device_default->ctl(device_default, DEVICE_CTL_RESET) < 0) {
fprintf(stderr, "prog: failed to reset after programming\n");
printc_err("prog: failed to reset after programming\n");
return -1;
}
@ -576,12 +576,12 @@ int cmd_setbreak(char **arg)
address_t addr;
if (!addr_text) {
fprintf(stderr, "setbreak: address required\n");
printc_err("setbreak: address required\n");
return -1;
}
if (expr_eval(stab_default, addr_text, &addr) < 0) {
fprintf(stderr, "setbreak: invalid address\n");
printc_err("setbreak: invalid address\n");
return -1;
}
@ -589,7 +589,7 @@ int cmd_setbreak(char **arg)
index = atoi(index_text);
if (index < 0 || index >= device_default->max_breakpoints) {
fprintf(stderr, "setbreak: invalid breakpoint "
printc_err("setbreak: invalid breakpoint "
"slot: %d\n", index);
return -1;
}
@ -597,12 +597,12 @@ int cmd_setbreak(char **arg)
index = device_setbrk(device_default, index, 1, addr);
if (index < 0) {
fprintf(stderr, "setbreak: all breakpoint slots are "
printc_err("setbreak: all breakpoint slots are "
"occupied\n");
return -1;
}
printf("Set breakpoint %d\n", index);
printc("Set breakpoint %d\n", index);
return 0;
}
@ -615,17 +615,17 @@ int cmd_delbreak(char **arg)
int index = atoi(index_text);
if (index < 0 || index >= device_default->max_breakpoints) {
fprintf(stderr, "delbreak: invalid breakpoint "
printc_err("delbreak: invalid breakpoint "
"slot: %d\n", index);
return -1;
}
printf("Clearing breakpoint %d\n", index);
printc("Clearing breakpoint %d\n", index);
device_setbrk(device_default, index, 0, 0);
} else {
int i;
printf("Clearing all breakpoints...\n");
printc("Clearing all breakpoints...\n");
for (i = 0; i < device_default->max_breakpoints; i++)
device_setbrk(device_default, i, 0, 0);
}
@ -637,7 +637,7 @@ int cmd_break(char **arg)
{
int i;
printf("%d breakpoints available:\n", device_default->max_breakpoints);
printc("%d breakpoints available:\n", device_default->max_breakpoints);
for (i = 0; i < device_default->max_breakpoints; i++) {
const struct device_breakpoint *bp =
&device_default->breakpoints[i];
@ -646,15 +646,15 @@ int cmd_break(char **arg)
char name[128];
address_t offset;
printf(" %d. 0x%05x", i, bp->addr);
printc(" %d. 0x%05x", i, bp->addr);
if (!stab_nearest(stab_default, bp->addr, name,
sizeof(name), &offset)) {
printf(" (%s", name);
printc(" (%s", name);
if (offset)
printf("+0x%x", offset);
printf(")");
printc("+0x%x", offset);
printc(")");
}
printf("\n");
printc("\n");
}
}

43
elf32.c
View File

@ -25,6 +25,7 @@
#include <elf.h>
#endif
#include "elf32.h"
#include "output.h"
#ifndef EM_MSP430
#define EM_MSP430 0x69
@ -51,12 +52,12 @@ static int read_ehdr(struct elf32_info *info, FILE *in)
/* Read and check the ELF header */
rewind(in);
if (fread(&info->file_ehdr, sizeof(info->file_ehdr), 1, in) < 0) {
perror("elf32: couldn't read ELF header");
pr_error("elf32: couldn't read ELF header");
return -1;
}
if (memcmp(info->file_ehdr.e_ident, elf32_id, sizeof(elf32_id))) {
fprintf(stderr, "elf32: not an ELF32 file\n");
printc_err("elf32: not an ELF32 file\n");
return -1;
}
@ -68,7 +69,7 @@ static int read_phdr(struct elf32_info *info, FILE *in)
int i;
if (info->file_ehdr.e_phnum > MAX_PHDRS) {
fprintf(stderr, "elf32: too many program headers: %d\n",
printc_err("elf32: too many program headers: %d\n",
info->file_ehdr.e_phnum);
return -1;
}
@ -77,13 +78,13 @@ static int read_phdr(struct elf32_info *info, FILE *in)
if (fseek(in, i * info->file_ehdr.e_phentsize +
info->file_ehdr.e_phoff,
SEEK_SET) < 0) {
fprintf(stderr, "elf32: can't seek to phdr %d\n", i);
printc_err("elf32: can't seek to phdr %d\n", i);
return -1;
}
if (fread(&info->file_phdrs[i],
sizeof(info->file_phdrs[0]), 1, in) < 0) {
fprintf(stderr, "elf32: can't read phdr %d: %s\n",
printc_err("elf32: can't read phdr %d: %s\n",
i, strerror(errno));
return -1;
}
@ -97,7 +98,7 @@ static int read_shdr(struct elf32_info *info, FILE *in)
int i;
if (info->file_ehdr.e_shnum > MAX_SHDRS) {
fprintf(stderr, "elf32: too many section headers: %d\n",
printc_err("elf32: too many section headers: %d\n",
info->file_ehdr.e_shnum);
return -1;
}
@ -106,13 +107,13 @@ static int read_shdr(struct elf32_info *info, FILE *in)
if (fseek(in, i * info->file_ehdr.e_shentsize +
info->file_ehdr.e_shoff,
SEEK_SET) < 0) {
fprintf(stderr, "elf32: can't seek to shdr %d\n", i);
printc_err("elf32: can't seek to shdr %d\n", i);
return -1;
}
if (fread(&info->file_shdrs[i],
sizeof(info->file_shdrs[0]), 1, in) < 0) {
fprintf(stderr, "elf32: can't read shdr %d: %s\n",
printc_err("elf32: can't read shdr %d: %s\n",
i, strerror(errno));
return -1;
}
@ -143,7 +144,7 @@ static int feed_section(struct elf32_info *info,
uint32_t addr = file_to_phys(info, offset);
if (fseek(in, offset, SEEK_SET) < 0) {
perror("elf32: can't seek to section");
pr_error("elf32: can't seek to section");
return -1;
}
@ -152,7 +153,7 @@ static int feed_section(struct elf32_info *info,
int len = fread(buf, 1, ask, in);
if (len < 0) {
perror("elf32: can't read section");
pr_error("elf32: can't read section");
return -1;
}
@ -175,7 +176,7 @@ static int read_all(struct elf32_info *info, FILE *in)
return -1;
if (info->file_ehdr.e_machine != EM_MSP430)
fprintf(stderr, "elf32: warning: unknown machine type: 0x%x\n",
printc_err("elf32: warning: unknown machine type: 0x%x\n",
info->file_ehdr.e_machine);
if (read_phdr(info, in) < 0)
@ -240,7 +241,7 @@ static int syms_load_strings(struct elf32_info *info, FILE *in, Elf32_Shdr *s)
return 0;
if (fseek(in, s->sh_offset, SEEK_SET) < 0) {
perror("elf32: can't seek to strings");
pr_error("elf32: can't seek to strings");
return -1;
}
@ -248,17 +249,17 @@ static int syms_load_strings(struct elf32_info *info, FILE *in, Elf32_Shdr *s)
info->string_tab = malloc(len + 1);
if (!info->string_tab) {
perror("elf32: can't allocate string table memory");
pr_error("elf32: can't allocate string table memory");
return -1;
}
if (!fread(info->string_tab, 1, info->string_len, in)) {
if (ferror(in)) {
perror("elf32: error reading strings");
pr_error("elf32: error reading strings");
return -1;
}
fprintf(stderr, "elf32: eof reading strings\n");
printc_err("elf32: eof reading strings\n");
return -1;
}
@ -275,7 +276,7 @@ static int syms_load_syms(struct elf32_info *info, FILE *in,
int len = s->sh_size / sizeof(syms[0]);
if (fseek(in, s->sh_offset, SEEK_SET) < 0) {
perror("elf32: can't seek to symbols");
pr_error("elf32: can't seek to symbols");
return -1;
}
@ -285,12 +286,12 @@ static int syms_load_syms(struct elf32_info *info, FILE *in,
int i;
if (!count) {
fprintf(stderr, "elf32: eof reading symbols\n");
printc_err("elf32: eof reading symbols\n");
return -1;
}
if (count < 0) {
perror("elf32: error reading symbols");
pr_error("elf32: error reading symbols");
return -1;
}
@ -300,7 +301,7 @@ static int syms_load_syms(struct elf32_info *info, FILE *in,
const char *name = info->string_tab + y->st_name;
if (y->st_name > info->string_len) {
fprintf(stderr, "elf32: symbol out of "
printc_err("elf32: symbol out of "
"bounds\n");
return -1;
}
@ -331,12 +332,12 @@ int elf32_syms(FILE *in, stab_t stab)
s = find_shdr(&info, SHT_SYMTAB);
if (!s) {
fprintf(stderr, "elf32: no symbol table\n");
printc_err("elf32: no symbol table\n");
return -1;
}
if (s->sh_link <= 0 || s->sh_link >= info.file_ehdr.e_shnum) {
fprintf(stderr, "elf32: no string table\n");
printc_err("elf32: no string table\n");
return -1;
}

25
expr.c
View File

@ -27,6 +27,7 @@
#include "expr.h"
#include "stab.h"
#include "util.h"
#include "output.h"
/************************************************************************
* Address expression parsing.
@ -46,7 +47,7 @@ static int addr_exp_data(stab_t stab,
address_t value;
if (!s->last_operator || s->last_operator == ')') {
fprintf(stderr, "syntax error at token %s\n", text);
printc_err("syntax error at token %s\n", text);
return -1;
}
@ -56,12 +57,12 @@ static int addr_exp_data(stab_t stab,
else if (isdigit(*text))
value = atoi(text);
else if (stab_get(stab, text, &value) < 0) {
fprintf(stderr, "can't parse token: %s\n", text);
printc_err("can't parse token: %s\n", text);
return -1;
}
if (s->data_stack_size + 1 > ARRAY_LEN(s->data_stack)) {
fprintf(stderr, "data stack overflow at token %s\n", text);
printc_err("data stack overflow at token %s\n", text);
return -1;
}
@ -117,7 +118,7 @@ static int addr_exp_pop(struct addr_exp_state *s)
return 0;
divzero:
fprintf(stderr, "divide by zero\n");
printc_err("divide by zero\n");
return -1;
}
@ -170,7 +171,7 @@ static int addr_exp_op(struct addr_exp_state *s, char op)
return -1;
if (!s->op_stack_size) {
fprintf(stderr, "parenthesis mismatch: )\n");
printc_err("parenthesis mismatch: )\n");
return -1;
}
@ -181,7 +182,7 @@ static int addr_exp_op(struct addr_exp_state *s, char op)
return -1;
if (s->op_stack_size + 1 > ARRAY_LEN(s->op_stack)) {
fprintf(stderr, "operator stack overflow: %c\n", op);
printc_err("operator stack overflow: %c\n", op);
return -1;
}
@ -192,20 +193,20 @@ static int addr_exp_op(struct addr_exp_state *s, char op)
return 0;
syntax_error:
fprintf(stderr, "syntax error at operator %c\n", op);
printc_err("syntax error at operator %c\n", op);
return -1;
}
static int addr_exp_finish(struct addr_exp_state *s, address_t *ret)
{
if (s->last_operator && s->last_operator != ')') {
fprintf(stderr, "syntax error at end of expression\n");
printc_err("syntax error at end of expression\n");
return -1;
}
while (s->op_stack_size) {
if (s->op_stack[s->op_stack_size - 1] == '(') {
fprintf(stderr, "parenthesis mismatch: (\n");
printc_err("parenthesis mismatch: (\n");
return -1;
}
@ -214,7 +215,7 @@ static int addr_exp_finish(struct addr_exp_state *s, address_t *ret)
}
if (s->data_stack_size != 1) {
fprintf(stderr, "no data: stack size is %d\n",
printc_err("no data: stack size is %d\n",
s->data_stack_size);
return -1;
}
@ -250,7 +251,7 @@ int expr_eval(stab_t stab, const char *text, address_t *addr)
*text == '$' || *text == ':')
cc = 3;
else {
fprintf(stderr, "illegal character in expression: %c\n",
printc_err("illegal character in expression: %c\n",
*text);
return -1;
}
@ -286,6 +287,6 @@ int expr_eval(stab_t stab, const char *text, address_t *addr)
return 0;
fail:
fprintf(stderr, "bad address expression: %s\n", text_save);
printc_err("bad address expression: %s\n", text_save);
return -1;
}

97
fet.c
View File

@ -31,6 +31,7 @@
#include "fet.h"
#include "fet_error.h"
#include "fet_db.h"
#include "output.h"
#define MAX_PARAMS 16
@ -238,7 +239,7 @@ static int parse_packet(struct fet_device *dev, int plen)
int error;
if (c != r) {
fprintf(stderr, "fet: checksum error (calc %04x,"
printc_err("fet: checksum error (calc %04x,"
" recv %04x)\n", c, r);
return -1;
}
@ -252,13 +253,13 @@ static int parse_packet(struct fet_device *dev, int plen)
error = dev->fet_buf[i++];
if (error) {
fprintf(stderr, "fet: FET returned error code %d (%s)\n",
printc_err("fet: FET returned error code %d (%s)\n",
error, fet_error(error));
return -1;
}
if (type == PTYPE_NAK) {
fprintf(stderr, "fet: FET returned NAK\n");
printc_err("fet: FET returned NAK\n");
return -1;
}
@ -273,7 +274,7 @@ static int parse_packet(struct fet_device *dev, int plen)
i += 2;
if (dev->fet_reply.argc >= MAX_PARAMS) {
fprintf(stderr, "fet: too many params: %d\n",
printc_err("fet: too many params: %d\n",
dev->fet_reply.argc);
return -1;
}
@ -308,7 +309,7 @@ static int parse_packet(struct fet_device *dev, int plen)
return 0;
too_short:
fprintf(stderr, "fet: too short (%d bytes)\n",
printc_err("fet: too short (%d bytes)\n",
plen);
return -1;
}
@ -463,7 +464,7 @@ static int xfer(struct fet_device *dev,
return -1;
if (dev->fet_reply.command_code != command_code) {
fprintf(stderr, "fet: reply type mismatch\n");
printc_err("fet: reply type mismatch\n");
return -1;
}
@ -476,9 +477,9 @@ static int xfer(struct fet_device *dev,
static void show_dev_info(const char *name, const struct fet_device *dev)
{
printf("Device: %s\n", name);
printf("Code memory starts at 0x%04x\n", dev->code_start);
printf("Number of breakpoints: %d\n", dev->base.max_breakpoints);
printc("Device: %s\n", name);
printc("Code memory starts at 0x%04x\n", dev->code_start);
printc("Number of breakpoints: %d\n", dev->base.max_breakpoints);
}
static int identify_old(struct fet_device *dev)
@ -489,7 +490,7 @@ static int identify_old(struct fet_device *dev)
return -1;
if (dev->fet_reply.datalen < 0x26) {
fprintf(stderr, "fet: missing info\n");
printc_err("fet: missing info\n");
return -1;
}
@ -509,16 +510,16 @@ static int identify_new(struct fet_device *dev, const char *force_id)
const struct fet_db_record *r;
if (xfer(dev, C_IDENT1, NULL, 0, 2, 0, 0) < 0) {
fprintf(stderr, "fet: command C_IDENT1 failed\n");
printc_err("fet: command C_IDENT1 failed\n");
return -1;
}
if (dev->fet_reply.datalen < 2) {
fprintf(stderr, "fet: missing info\n");
printc_err("fet: missing info\n");
return -1;
}
printf("Device ID: 0x%02x%02x\n",
printc("Device ID: 0x%02x%02x\n",
dev->fet_reply.data[0], dev->fet_reply.data[1]);
if (force_id)
@ -528,7 +529,7 @@ static int identify_new(struct fet_device *dev, const char *force_id)
dev->fet_reply.datalen);
if (!r) {
fprintf(stderr, "fet: unknown device\n");
printc_err("fet: unknown device\n");
debug_hexdump("msg28_data:", dev->fet_reply.data,
dev->fet_reply.datalen);
return -1;
@ -540,12 +541,12 @@ static int identify_new(struct fet_device *dev, const char *force_id)
show_dev_info(r->name, dev);
if (xfer(dev, C_IDENT3, r->msg2b_data, r->msg2b_len, 0) < 0)
fprintf(stderr, "fet: warning: message C_IDENT3 failed\n");
printc_err("fet: warning: message C_IDENT3 failed\n");
if (xfer(dev, C_IDENT2, r->msg29_data, FET_DB_MSG29_LEN,
3, r->msg29_params[0], r->msg29_params[1],
r->msg29_params[2]) < 0) {
fprintf(stderr, "fet: message C_IDENT2 failed\n");
printc_err("fet: message C_IDENT2 failed\n");
return -1;
}
@ -566,7 +567,7 @@ static int do_identify(struct fet_device *dev, const char *force_id)
static int do_run(struct fet_device *dev, int type)
{
if (xfer(dev, C_RUN, NULL, 0, 2, type, 0) < 0) {
fprintf(stderr, "fet: failed to restart CPU\n");
printc_err("fet: failed to restart CPU\n");
return -1;
}
@ -576,23 +577,23 @@ static int do_run(struct fet_device *dev, int type)
static int do_erase(struct fet_device *dev)
{
if (xfer(dev, C_RESET, NULL, 0, 3, FET_RESET_ALL, 0, 0) < 0) {
fprintf(stderr, "fet: reset before erase failed\n");
printc_err("fet: reset before erase failed\n");
return -1;
}
if (xfer(dev, C_CONFIGURE, NULL, 0, 2, FET_CONFIG_CLKCTRL, 0x26) < 0) {
fprintf(stderr, "fet: config (1) failed\n");
printc_err("fet: config (1) failed\n");
return -1;
}
if (xfer(dev, C_CONFIGURE, NULL, 0, 2, FET_CONFIG_FLASH_LOCK, 0) < 0) {
fprintf(stderr, "fet: config (2) failed\n");
printc_err("fet: config (2) failed\n");
return -1;
}
if (xfer(dev, C_ERASE, NULL, 0, 3, FET_ERASE_MAIN,
dev->code_start, 0) < 0) {
fprintf(stderr, "fet: erase command failed\n");
printc_err("fet: erase command failed\n");
return -1;
}
@ -608,7 +609,7 @@ static device_status_t fet_poll(device_t dev_base)
return DEVICE_STATUS_INTR;
if (xfer(dev, C_STATE, NULL, 0, 1, 0) < 0) {
fprintf(stderr, "fet: polling failed\n");
printc_err("fet: polling failed\n");
return DEVICE_STATUS_ERROR;
}
@ -633,7 +634,7 @@ static int refresh_bps(struct fet_device *dev)
addr = 0;
if (xfer(dev, C_BREAKPOINT, NULL, 0, 2, i, addr) < 0) {
fprintf(stderr, "fet: failed to refresh "
printc_err("fet: failed to refresh "
"breakpoint #%d\n", i);
ret = -1;
} else {
@ -652,20 +653,20 @@ static int fet_ctl(device_t dev_base, device_ctl_t action)
switch (action) {
case DEVICE_CTL_RESET:
if (xfer(dev, C_RESET, NULL, 0, 3, FET_RESET_ALL, 0, 0) < 0) {
fprintf(stderr, "fet: reset failed\n");
printc_err("fet: reset failed\n");
return -1;
}
break;
case DEVICE_CTL_RUN:
if (refresh_bps(dev) < 0)
fprintf(stderr, "warning: fet: failed to refresh "
printc_err("warning: fet: failed to refresh "
"breakpoints\n");
return do_run(dev, FET_RUN_BREAKPOINT);
case DEVICE_CTL_HALT:
if (xfer(dev, C_STATE, NULL, 0, 1, 1) < 0) {
fprintf(stderr, "fet: failed to halt CPU\n");
printc_err("fet: failed to halt CPU\n");
return -1;
}
break;
@ -698,10 +699,10 @@ static void fet_destroy(device_t dev_base)
struct fet_device *dev = (struct fet_device *)dev_base;
if (xfer(dev, C_RUN, NULL, 0, 2, FET_RUN_FREE, 1) < 0)
fprintf(stderr, "fet: failed to restart CPU\n");
printc_err("fet: failed to restart CPU\n");
if (xfer(dev, C_CLOSE, NULL, 0, 1, 0) < 0)
fprintf(stderr, "fet: close command failed\n");
printc_err("fet: close command failed\n");
dev->transport->destroy(dev->transport);
free(dev);
@ -716,13 +717,13 @@ int fet_readmem(device_t dev_base, address_t addr, uint8_t *buffer,
int plen = count > 128 ? 128 : count;
if (xfer(dev, C_READMEMORY, NULL, 0, 2, addr, plen) < 0) {
fprintf(stderr, "fet: failed to read "
printc_err("fet: failed to read "
"from 0x%04x\n", addr);
return -1;
}
if (dev->fet_reply.datalen < plen) {
fprintf(stderr, "fet: short data: "
printc_err("fet: short data: "
"%d bytes\n", dev->fet_reply.datalen);
return -1;
}
@ -748,7 +749,7 @@ int fet_writemem(device_t dev_base, address_t addr,
ret = xfer(dev, C_WRITEMEMORY, buffer, plen, 1, addr);
if (ret < 0) {
fprintf(stderr, "fet: failed to write to 0x%04x\n",
printc_err("fet: failed to write to 0x%04x\n",
addr);
return -1;
}
@ -770,7 +771,7 @@ static int fet_getregs(device_t dev_base, address_t *regs)
return -1;
if (dev->fet_reply.datalen < DEVICE_NUM_REGS * 4) {
fprintf(stderr, "fet: short reply (%d bytes)\n",
printc_err("fet: short reply (%d bytes)\n",
dev->fet_reply.datalen);
return -1;
}
@ -800,7 +801,7 @@ static int fet_setregs(device_t dev_base, const address_t *regs)
ret = xfer(dev, C_WRITEREGISTERS, buf, sizeof(buf), 1, 0xffff);
if (ret < 0) {
fprintf(stderr, "fet: context set failed\n");
printc_err("fet: context set failed\n");
return -1;
}
@ -812,30 +813,30 @@ static int do_configure(struct fet_device *dev)
if (dev->proto_flags & FET_PROTO_SPYBIWIRE) {
if (!xfer(dev, C_CONFIGURE, NULL, 0,
2, FET_CONFIG_PROTOCOL, 1)) {
printf("Configured for Spy-Bi-Wire\n");
printc("Configured for Spy-Bi-Wire\n");
return 0;
}
fprintf(stderr, "fet: Spy-Bi-Wire configuration failed\n");
printc_err("fet: Spy-Bi-Wire configuration failed\n");
return -1;
}
if (!xfer(dev, C_CONFIGURE, NULL, 0,
2, FET_CONFIG_PROTOCOL, 2)) {
printf("Configured for JTAG (2)\n");
printc("Configured for JTAG (2)\n");
return 0;
}
fprintf(stderr, "fet: warning: JTAG configuration failed -- "
printc_err("fet: warning: JTAG configuration failed -- "
"retrying\n");
if (!xfer(dev, C_CONFIGURE, NULL, 0,
2, FET_CONFIG_PROTOCOL, 0)) {
printf("Configured for JTAG (0)\n");
printc("Configured for JTAG (0)\n");
return 0;
}
fprintf(stderr, "fet: JTAG configuration failed\n");
printc_err("fet: JTAG configuration failed\n");
return -1;
}
@ -846,7 +847,7 @@ device_t fet_open(transport_t transport, int proto_flags, int vcc_mv,
int i;
if (!dev) {
perror("fet: failed to allocate memory");
pr_error("fet: failed to allocate memory");
return NULL;
}
@ -864,24 +865,24 @@ device_t fet_open(transport_t transport, int proto_flags, int vcc_mv,
dev->proto_flags = proto_flags;
if (proto_flags & FET_PROTO_OLIMEX) {
printf("Resetting Olimex command processor...\n");
printc("Resetting Olimex command processor...\n");
transport->send(dev->transport, (const uint8_t *)"\x7e", 1);
usleep(5000);
transport->send(dev->transport, (const uint8_t *)"\x7e", 1);
usleep(5000);
}
printf("Initializing FET...\n");
printc("Initializing FET...\n");
if (xfer(dev, C_INITIALIZE, NULL, 0, 0) < 0) {
fprintf(stderr, "fet: open failed\n");
printc_err("fet: open failed\n");
goto fail;
}
dev->version = dev->fet_reply.argv[0];
printf("FET protocol version is %d\n", dev->version);
printc("FET protocol version is %d\n", dev->version);
if (xfer(dev, 0x27, NULL, 0, 1, 4) < 0) {
fprintf(stderr, "fet: init failed\n");
printc_err("fet: init failed\n");
goto fail;
}
@ -890,13 +891,13 @@ device_t fet_open(transport_t transport, int proto_flags, int vcc_mv,
/* set VCC */
if (xfer(dev, C_VCC, NULL, 0, 1, vcc_mv) < 0)
fprintf(stderr, "warning: fet: set VCC failed\n");
printc_err("warning: fet: set VCC failed\n");
else
printf("Set Vcc: %d mV\n", vcc_mv);
printc("Set Vcc: %d mV\n", vcc_mv);
/* Identify the chip */
if (do_identify(dev, force_id) < 0) {
fprintf(stderr, "fet: identify failed\n");
printc_err("fet: identify failed\n");
goto fail;
}

77
gdb.c
View File

@ -31,6 +31,7 @@
#include "util.h"
#include "opdb.h"
#include "gdb.h"
#include "output.h"
#define MAX_MEM_XFER 1024
@ -78,7 +79,7 @@ static int gdb_read(struct gdb_data *data, int blocking)
if (select(data->sock + 1, &r, NULL, NULL,
blocking ? NULL : &to) < 0) {
perror("gdb: select");
pr_error("gdb: select");
return -1;
}
@ -89,12 +90,12 @@ static int gdb_read(struct gdb_data *data, int blocking)
if (len < 0) {
data->error = errno;
perror("gdb: recv");
pr_error("gdb: recv");
return -1;
}
if (!len) {
printf("Connection closed\n");
printc("Connection closed\n");
return -1;
}
@ -129,7 +130,7 @@ static int gdb_flush(struct gdb_data *data)
{
if (send(data->sock, data->outbuf, data->outlen, 0) < 0) {
data->error = errno;
perror("gdb: flush");
pr_error("gdb: flush");
return -1;
}
@ -144,11 +145,11 @@ static int gdb_flush_ack(struct gdb_data *data)
do {
data->outbuf[data->outlen] = 0;
#ifdef DEBUG_GDB
printf("-> %s\n", data->outbuf);
printc("-> %s\n", data->outbuf);
#endif
if (send(data->sock, data->outbuf, data->outlen, 0) < 0) {
data->error = errno;
perror("gdb: flush_ack");
pr_error("gdb: flush_ack");
return -1;
}
@ -215,7 +216,7 @@ static int read_registers(struct gdb_data *data)
address_t regs[DEVICE_NUM_REGS];
int i;
printf("Reading registers\n");
printc("Reading registers\n");
if (device_default->getregs(device_default, regs) < 0)
return gdb_send(data, "E00");
@ -240,14 +241,14 @@ static int monitor_command(struct gdb_data *data, char *buf)
}
cmd[len] = 0;
printf("Monitor command received: %s\n", cmd);
printc("Monitor command received: %s\n", cmd);
if (!strcasecmp(cmd, "reset")) {
printf("Resetting device\n");
printc("Resetting device\n");
if (device_default->ctl(device_default, DEVICE_CTL_RESET) < 0)
return gdb_send_hex(data, "Reset failed\n");
} else if (!strcasecmp(cmd, "erase")) {
printf("Erasing device\n");
printc("Erasing device\n");
if (device_default->ctl(device_default, DEVICE_CTL_ERASE) < 0)
return gdb_send_hex(data, "Erase failed\n");
}
@ -263,7 +264,7 @@ static int write_registers(struct gdb_data *data, char *buf)
if (strlen(buf) < DEVICE_NUM_REGS * 4)
return gdb_send(data, "E00");
printf("Writing registers\n");
printc("Writing registers\n");
for (i = 0; i < DEVICE_NUM_REGS; i++) {
regs[i] = (hexval(buf[2]) << 12) |
(hexval(buf[3]) << 8) |
@ -286,7 +287,7 @@ static int read_memory(struct gdb_data *data, char *text)
int i;
if (!length_text) {
fprintf(stderr, "gdb: malformed memory read request\n");
printc_err("gdb: malformed memory read request\n");
return gdb_send(data, "E00");
}
@ -298,7 +299,7 @@ static int read_memory(struct gdb_data *data, char *text)
if (length > sizeof(buf))
length = sizeof(buf);
printf("Reading %d bytes from 0x%04x\n", length, addr);
printc("Reading %d bytes from 0x%04x\n", length, addr);
if (device_default->readmem(device_default, addr, buf, length) < 0)
return gdb_send(data, "E00");
@ -320,7 +321,7 @@ static int write_memory(struct gdb_data *data, char *text)
int buflen = 0;
if (!(data_text && length_text)) {
fprintf(stderr, "gdb: malformed memory write request\n");
printc_err("gdb: malformed memory write request\n");
return gdb_send(data, "E00");
}
@ -337,11 +338,11 @@ static int write_memory(struct gdb_data *data, char *text)
}
if (buflen != length) {
fprintf(stderr, "gdb: length mismatch\n");
printc_err("gdb: length mismatch\n");
return gdb_send(data, "E00");
}
printf("Writing %d bytes to 0x%04x\n", buflen, addr);
printc("Writing %d bytes to 0x%04x\n", buflen, addr);
if (device_default->writemem(device_default, addr, buf, buflen) < 0)
return gdb_send(data, "E00");
@ -394,7 +395,7 @@ static int run_final_status(struct gdb_data *data)
static int single_step(struct gdb_data *data, char *buf)
{
printf("Single stepping\n");
printc("Single stepping\n");
if (run_set_pc(data, buf) < 0 ||
device_default->ctl(device_default, DEVICE_CTL_STEP) < 0)
@ -405,7 +406,7 @@ static int single_step(struct gdb_data *data, char *buf)
static int run(struct gdb_data *data, char *buf)
{
printf("Running\n");
printc("Running\n");
if (run_set_pc(data, buf) < 0 ||
device_default->ctl(device_default, DEVICE_CTL_RUN) < 0)
@ -418,7 +419,7 @@ static int run(struct gdb_data *data, char *buf)
return gdb_send(data, "E00");
if (status == DEVICE_STATUS_HALTED) {
printf("Target halted\n");
printc("Target halted\n");
goto out;
}
@ -432,7 +433,7 @@ static int run(struct gdb_data *data, char *buf)
return -1;
if (c == 3) {
printf("Interrupted by gdb\n");
printc("Interrupted by gdb\n");
goto out;
}
}
@ -458,21 +459,21 @@ static int set_breakpoint(struct gdb_data *data, int enable, char *buf)
/* Make sure there's a type argument */
if (!parts[0]) {
fprintf(stderr, "gdb: breakpoint requested with no type\n");
printc_err("gdb: breakpoint requested with no type\n");
return gdb_send(data, "E00");
}
/* We only support breakpoints */
type = atoi(parts[0]);
if (type < 0 || type > 1) {
fprintf(stderr, "gdb: unsupported breakpoint type: %s\n",
printc_err("gdb: unsupported breakpoint type: %s\n",
parts[0]);
return gdb_send(data, "");
}
/* There needs to be an address specified */
if (!parts[1]) {
fprintf(stderr, "gdb: breakpoint address missing\n");
printc_err("gdb: breakpoint address missing\n");
return gdb_send(data, "E00");
}
@ -481,15 +482,15 @@ static int set_breakpoint(struct gdb_data *data, int enable, char *buf)
if (enable) {
if (device_setbrk(device_default, -1, 1, addr) < 0) {
fprintf(stderr, "gdb: can't add breakpoint at "
printc_err("gdb: can't add breakpoint at "
"0x%04x\n", addr);
return gdb_send(data, "E00");
}
printf("Breakpoint set at 0x%04x\n", addr);
printc("Breakpoint set at 0x%04x\n", addr);
} else {
device_setbrk(device_default, -1, 0, addr);
printf("Breakpoint cleared at 0x%04x\n", addr);
printc("Breakpoint cleared at 0x%04x\n", addr);
}
return gdb_send(data, "OK");
@ -583,13 +584,13 @@ static void gdb_reader_loop(struct gdb_data *data)
cksum_recv = (cksum_recv << 4) | hexval(c);
#ifdef DEBUG_GDB
printf("<- $%s#%02x\n", buf, cksum_recv);
printc("<- $%s#%02x\n", buf, cksum_recv);
#endif
if (cksum_recv != cksum_calc) {
fprintf(stderr, "gdb: bad checksum (calc = 0x%02x, "
printc_err("gdb: bad checksum (calc = 0x%02x, "
"recv = 0x%02x)\n", cksum_calc, cksum_recv);
fprintf(stderr, "gdb: packet data was: %s\n", buf);
printc_err("gdb: packet data was: %s\n", buf);
gdb_printf(data, "-");
if (gdb_flush(data) < 0)
return;
@ -618,42 +619,42 @@ static int gdb_server(int port)
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0) {
perror("gdb: can't create socket");
pr_error("gdb: can't create socket");
return -1;
}
arg = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &arg, sizeof(arg)) < 0)
perror("gdb: warning: can't reuse socket address");
pr_error("gdb: warning: can't reuse socket address");
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
fprintf(stderr, "gdb: can't bind to port %d: %s\n",
printc_err("gdb: can't bind to port %d: %s\n",
port, strerror(errno));
close(sock);
return -1;
}
if (listen(sock, 1) < 0) {
perror("gdb: can't listen on socket");
pr_error("gdb: can't listen on socket");
close(sock);
return -1;
}
printf("Bound to port %d. Now waiting for connection...\n", port);
printc("Bound to port %d. Now waiting for connection...\n", port);
len = sizeof(addr);
client = accept(sock, (struct sockaddr *)&addr, &len);
if (client < 0) {
perror("gdb: failed to accept connection");
pr_error("gdb: failed to accept connection");
close(sock);
return -1;
}
close(sock);
printf("Client connected from %s:%d\n",
printc("Client connected from %s:%d\n",
inet_ntoa(addr.sin_addr), htons(addr.sin_port));
data.sock = client;
@ -663,7 +664,7 @@ static int gdb_server(int port)
data.outlen = 0;
/* Put the hardware breakpoint setting into a known state. */
printf("Clearing all breakpoints...\n");
printc("Clearing all breakpoints...\n");
for (i = 0; i < device_default->max_breakpoints; i++)
device_setbrk(device_default, i, 0, 0);
@ -681,7 +682,7 @@ int cmd_gdb(char **arg)
port = atoi(port_text);
if (port <= 0 || port > 65535) {
fprintf(stderr, "gdb: invalid port: %d\n", port);
printc_err("gdb: invalid port: %d\n", port);
return -1;
}

13
ihex.c
View File

@ -20,6 +20,7 @@
#include <string.h>
#include <ctype.h>
#include "ihex.h"
#include "output.h"
int ihex_check(FILE *in)
{
@ -46,7 +47,7 @@ static int feed_line(FILE *in, uint8_t *data, int nbytes, binfile_imgcb_t cb,
cksum = ~(cksum - 1) & 0xff;
if (data[nbytes - 1] != cksum) {
fprintf(stderr, "ihex: invalid checksum: %02x "
printc_err("ihex: invalid checksum: %02x "
"(calculated %02x)\n", data[nbytes - 1], cksum);
return -1;
}
@ -66,7 +67,7 @@ static int feed_line(FILE *in, uint8_t *data, int nbytes, binfile_imgcb_t cb,
case 2:
if (data_len != 2) {
fprintf(stderr, "ihex: invalid 02 record\n");
printc_err("ihex: invalid 02 record\n");
return -1;
}
@ -76,7 +77,7 @@ static int feed_line(FILE *in, uint8_t *data, int nbytes, binfile_imgcb_t cb,
case 4:
if (data_len != 2) {
fprintf(stderr, "ihex: invalid 04 record\n");
printc_err("ihex: invalid 04 record\n");
return -1;
}
@ -85,7 +86,7 @@ static int feed_line(FILE *in, uint8_t *data, int nbytes, binfile_imgcb_t cb,
break;
default:
fprintf(stderr, "warning: ihex: unknown record type: "
printc_err("warning: ihex: unknown record type: "
"0x%02x\n", type);
break;
}
@ -108,7 +109,7 @@ int ihex_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
lno++;
if (buf[0] != ':') {
fprintf(stderr, "ihex: line %d: invalid start "
printc_err("ihex: line %d: invalid start "
"marker\n", lno);
continue;
}
@ -129,7 +130,7 @@ int ihex_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
/* Handle the line */
if (feed_line(in, data, nbytes, cb, user_data,
&segment_offset) < 0) {
fprintf(stderr, "ihex: error on line %d\n", lno);
printc_err("ihex: error on line %d\n", lno);
return -1;
}
}

54
main.c
View File

@ -37,6 +37,7 @@
#include "expr.h"
#include "opdb.h"
#include "reader.h"
#include "output.h"
#include "sim.h"
#include "bsl.h"
@ -55,19 +56,19 @@ static void io_prefix(const char *prefix, uint16_t pc,
address_t offset;
if (!stab_nearest(stab_default, pc, name, sizeof(name), &offset)) {
printf("%s", name);
printc("%s", name);
if (offset)
printf("+0x%x", offset);
printc("+0x%x", offset);
} else {
printf("0x%04x", pc);
printc("0x%04x", pc);
}
printf(": IO %s.%c: 0x%04x", prefix, is_byte ? 'B' : 'W', addr);
printc(": IO %s.%c: 0x%04x", prefix, is_byte ? 'B' : 'W', addr);
if (!stab_nearest(stab_default, addr, name, sizeof(name), &offset)) {
printf(" (%s", name);
printc(" (%s", name);
if (offset)
printf("+0x%x", offset);
printf(")");
printc("+0x%x", offset);
printc(")");
}
}
@ -81,10 +82,10 @@ static int fetch_io(void *user_data, uint16_t pc,
int len;
address_t data;
printf("? ");
printc("? ");
fflush(stdout);
if (!fgets(text, sizeof(text), stdin)) {
printf("\nAborted IO request\n");
printc("\nAborted IO request\n");
return -1;
}
@ -112,9 +113,9 @@ static void store_io(void *user_data, uint16_t pc,
io_prefix("WRITE", pc, addr, is_byte);
if (is_byte)
printf(" => 0x%02x\n", data & 0xff);
printc(" => 0x%02x\n", data & 0xff);
else
printf(" => 0x%04x\n", data);
printc(" => 0x%04x\n", data);
}
struct cmdline_args {
@ -155,7 +156,7 @@ static device_t driver_open_rf2500(const struct cmdline_args *args)
transport_t trans;
if (args->serial_device) {
fprintf(stderr, "This driver does not support tty devices.\n");
printc_err("This driver does not support tty devices.\n");
return NULL;
}
@ -191,8 +192,8 @@ static device_t driver_open_uif(const struct cmdline_args *args)
transport_t trans;
if (!args->serial_device) {
fprintf(stderr, "This driver does not support USB access. "
"Specify a tty device using -d.\n");
printc_err("This driver does not support USB access. "
"Specify a tty device using -d.\n");
return NULL;
}
@ -206,8 +207,8 @@ static device_t driver_open_uif(const struct cmdline_args *args)
static device_t driver_open_uif_bsl(const struct cmdline_args *args)
{
if (!args->serial_device) {
fprintf(stderr, "This driver does not support USB access. "
"Specify a tty device using -d.\n");
printc_err("This driver does not support USB access. "
"Specify a tty device using -d.\n");
return NULL;
}
@ -246,8 +247,7 @@ static void usage(const char *progname)
{
int i;
fprintf(stderr,
"Usage: %s [options] <driver> [command ...]\n"
printc_err("Usage: %s [options] <driver> [command ...]\n"
"\n"
" -d device\n"
" Connect via the given tty device, rather than USB.\n"
@ -275,11 +275,11 @@ static void usage(const char *progname)
"command reader is started.\n\n",
progname);
printf("Available drivers are:\n");
printc("Available drivers are:\n");
for (i = 0; i < ARRAY_LEN(driver_table); i++) {
const struct driver *drv = &driver_table[i];
printf(" %s\n %s\n", drv->name, drv->help);
printc(" %s\n %s\n", drv->name, drv->help);
}
}
@ -315,16 +315,16 @@ static int list_devices(void)
vector_init(&v, sizeof(const char *));
if (fet_db_enum(add_fet_device, &v) < 0) {
perror("couldn't allocate memory");
pr_error("couldn't allocate memory");
vector_destroy(&v);
return -1;
}
qsort(v.ptr, v.size, v.elemsize, cmp_char_ptr);
printf("Devices supported by FET driver:\n");
printc("Devices supported by FET driver:\n");
for (i = 0; i < v.size; i++)
printf(" %s\n", VECTOR_AT(v, i, const char *));
printc(" %s\n", VECTOR_AT(v, i, const char *));
vector_destroy(&v);
return 0;
@ -384,18 +384,18 @@ static int parse_cmdline_args(int argc, char **argv,
break;
case '?':
fprintf(stderr, "Try --help for usage information.\n");
printc_err("Try --help for usage information.\n");
return -1;
}
if (args->usb_device && args->serial_device) {
fprintf(stderr, "You can't simultaneously specify a serial and "
printc_err("You can't simultaneously specify a serial and "
"a USB device.\n");
return -1;
}
if (optind >= argc) {
fprintf(stderr, "You need to specify a driver. Try --help for "
printc_err("You need to specify a driver. Try --help for "
"a list.\n");
return -1;
}
@ -415,7 +415,7 @@ int setup_driver(struct cmdline_args *args)
strcasecmp(driver_table[i].name, args->driver_name))
i++;
if (i >= ARRAY_LEN(driver_table)) {
fprintf(stderr, "Unknown driver: %s. Try --help for a list.\n",
printc_err("Unknown driver: %s. Try --help for a list.\n",
args->driver_name);
return -1;
}

View File

@ -24,6 +24,7 @@
#include "olimex.h"
#include "util.h"
#include "usbutil.h"
#include "output.h"
struct olimex_transport {
struct transport base;
@ -70,31 +71,31 @@ static int open_interface(struct olimex_transport *tr,
char drName[256];
#endif
printf(__FILE__": Trying to open interface %d on %s\n",
printc(__FILE__": Trying to open interface %d on %s\n",
ino, dev->filename);
tr->int_number = ino;
tr->handle = usb_open(dev);
if (!tr->handle) {
perror(__FILE__": can't open device");
pr_error(__FILE__": can't open device");
return -1;
}
#if !(defined(__APPLE__) || defined(WIN32))
drv = usb_get_driver_np(tr->handle, tr->int_number, drName,
sizeof(drName));
printf(__FILE__" : driver %d\n", drv);
printc(__FILE__" : driver %d\n", drv);
if (drv >= 0) {
if (usb_detach_kernel_driver_np(tr->handle,
tr->int_number) < 0)
perror(__FILE__": warning: can't detach "
pr_error(__FILE__": warning: can't detach "
"kernel driver");
}
#endif
if (usb_claim_interface(tr->handle, tr->int_number) < 0) {
perror(__FILE__": can't claim interface");
pr_error(__FILE__": can't claim interface");
usb_close(tr->handle);
return -1;
}
@ -102,14 +103,14 @@ static int open_interface(struct olimex_transport *tr,
int ret = usb_control_msg(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE,
CP210X_IFC_ENABLE, 0x1, 0, NULL, 0, 300);
#ifdef DEBUG_OLIMEX
printf(__FILE__": %s : Sending control message ret %d\n",
printc(__FILE__": %s : Sending control message ret %d\n",
__FUNCTION__, ret);
#endif
/* Set the baud rate to 500000 bps */
ret = usb_control_msg(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE,
CP210X_SET_BAUDDIV, 0x7, 0, NULL, 0, 300);
#ifdef DEBUG_OLIMEX
printf(__FILE__": %s : Sending control message ret %d\n",
printc(__FILE__": %s : Sending control message ret %d\n",
__FUNCTION__, ret);
#endif
/* Set the modem control settings.
@ -118,7 +119,7 @@ static int open_interface(struct olimex_transport *tr,
ret = usb_control_msg(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE,
CP210X_SET_MHS, 0x303, 0, NULL, 0, 300);
#ifdef DEBUG_OLIMEX
printf(__FILE__": %s : Sending control message ret %d\n",
printc(__FILE__": %s : Sending control message ret %d\n",
__FUNCTION__, ret);
#endif
@ -155,7 +156,7 @@ static int usbtr_send(transport_t tr_base, const uint8_t *data, int len)
sent = usb_bulk_write(tr->handle, USB_FET_OUT_EP,
(char *)data, len, TIMEOUT);
if (sent < 0) {
perror(__FILE__": can't send data");
pr_error(__FILE__": can't send data");
return -1;
}
@ -171,18 +172,18 @@ static int usbtr_recv(transport_t tr_base, uint8_t *databuf, int max_len)
int rlen;
#ifdef DEBUG_OLIMEX
printf(__FILE__": %s : read max %d\n", __FUNCTION__, max_len);
printc(__FILE__": %s : read max %d\n", __FUNCTION__, max_len);
#endif
rlen = usb_bulk_read(tr->handle, USB_FET_IN_EP, (char *)databuf,
max_len, TIMEOUT);
#ifdef DEBUG_OLIMEX
printf(__FILE__": %s : read %d\n", __FUNCTION__, rlen);
printc(__FILE__": %s : read %d\n", __FUNCTION__, rlen);
#endif
if (rlen < 0) {
perror(__FILE__": can't receive data");
pr_error(__FILE__": can't receive data");
return -1;
}
@ -209,7 +210,7 @@ transport_t olimex_open(const char *devpath)
char buf[64];
if (!tr) {
perror(__FILE__": can't allocate memory");
pr_error(__FILE__": can't allocate memory");
return NULL;
}
@ -232,7 +233,7 @@ transport_t olimex_open(const char *devpath)
}
if (open_device(tr, dev) < 0) {
fprintf(stderr, __FILE__ ": failed to open Olimex device\n");
printc_err(__FILE__ ": failed to open Olimex device\n");
return NULL;
}

View File

@ -18,6 +18,8 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include "opdb.h"
#include "output.h"
@ -83,3 +85,8 @@ int printc_err(const char *fmt, ...)
return write_text(&stderr_buf, buf, stderr);
}
void pr_error(const char *prefix)
{
printc_err("%s: %s\n", prefix, strerror(errno));
}

View File

@ -28,4 +28,6 @@
int printc(const char *fmt, ...);
int printc_err(const char *fmt, ...);
void pr_error(const char *prefix);
#endif

View File

@ -56,12 +56,12 @@ int prompt_abort(int flags)
return 0;
for (;;) {
printf("Symbols have not been saved since modification. "
printc("Symbols have not been saved since modification. "
"Continue (y/n)? ");
fflush(stdout);
if (!fgets(buf, sizeof(buf), stdin)) {
printf("\n");
printc("\n");
return 1;
}
@ -70,7 +70,7 @@ int prompt_abort(int flags)
if (toupper(buf[0]) == 'N')
return 1;
printf("Please answer \"y\" or \"n\".\n");
printc("Please answer \"y\" or \"n\".\n");
}
return 0;
@ -84,12 +84,12 @@ static char *readline(const char *prompt)
char *buf = malloc(LINE_BUF_SIZE);
if (!buf) {
perror("readline: can't allocate memory");
pr_error("readline: can't allocate memory");
return NULL;
}
for (;;) {
printf("(mspdebug) ");
printc("(mspdebug) ");
fflush(stdout);
if (fgets(buf, LINE_BUF_SIZE, stdin))
@ -98,7 +98,7 @@ static char *readline(const char *prompt)
if (feof(stdin))
break;
printf("\n");
printc("\n");
}
free(buf);
@ -132,7 +132,7 @@ static int do_command(char *arg, int interactive)
return ret;
}
fprintf(stderr, "unknown command: %s (try \"help\")\n",
printc_err("unknown command: %s (try \"help\")\n",
cmd_text);
return -1;
}
@ -146,9 +146,9 @@ void reader_loop(void)
in_reader_loop = 1;
printf("\n");
printc("\n");
cmd_help(NULL);
printf("\n");
printc("\n");
do {
for (;;) {
@ -163,7 +163,7 @@ void reader_loop(void)
}
} while (prompt_abort(MODIFY_SYMS));
printf("\n");
printc("\n");
in_reader_loop = old;
}
@ -180,7 +180,7 @@ int process_file(const char *filename)
in = fopen(filename, "r");
if (!in) {
fprintf(stderr, "read: can't open %s: %s\n",
printc_err("read: can't open %s: %s\n",
filename, strerror(errno));
return -1;
}
@ -197,7 +197,7 @@ int process_file(const char *filename)
continue;
if (do_command(cmd, 0) < 0) {
fprintf(stderr, "read: error processing %s (line %d)\n",
printc_err("read: error processing %s (line %d)\n",
filename, line_no);
fclose(in);
return -1;

View File

@ -23,6 +23,7 @@
#include "rf2500.h"
#include "util.h"
#include "usbutil.h"
#include "output.h"
struct rf2500_transport {
struct transport base;
@ -56,24 +57,24 @@ struct rf2500_transport {
static int open_interface(struct rf2500_transport *tr,
struct usb_device *dev, int ino)
{
printf("Trying to open interface %d on %s\n", ino, dev->filename);
printc("Trying to open interface %d on %s\n", ino, dev->filename);
tr->int_number = ino;
tr->handle = usb_open(dev);
if (!tr->handle) {
perror("rf2500: can't open device");
pr_error("rf2500: can't open device");
return -1;
}
#if !(defined(__APPLE__) || defined(WIN32))
if (usb_detach_kernel_driver_np(tr->handle, tr->int_number) < 0)
perror("rf2500: warning: can't "
pr_error("rf2500: warning: can't "
"detach kernel driver");
#endif
if (usb_claim_interface(tr->handle, tr->int_number) < 0) {
perror("rf2500: can't claim interface");
pr_error("rf2500: can't claim interface");
usb_close(tr->handle);
return -1;
}
@ -126,7 +127,7 @@ static int usbtr_send(transport_t tr_base, const uint8_t *data, int len)
#endif
if (usb_bulk_write(tr->handle, USB_FET_OUT_EP,
(char *)pbuf, txlen, 10000) < 0) {
perror("rf2500: can't send data");
pr_error("rf2500: can't send data");
return -1;
}
@ -146,7 +147,7 @@ static int usbtr_recv(transport_t tr_base, uint8_t *databuf, int max_len)
if (usb_bulk_read(tr->handle, USB_FET_IN_EP,
(char *)tr->buf, sizeof(tr->buf),
10000) < 0) {
perror("rf2500: can't receive data");
pr_error("rf2500: can't receive data");
return -1;
}
@ -185,7 +186,7 @@ transport_t rf2500_open(const char *devpath)
char buf[64];
if (!tr) {
perror("rf2500: can't allocate memory");
pr_error("rf2500: can't allocate memory");
return NULL;
}
@ -208,7 +209,7 @@ transport_t rf2500_open(const char *devpath)
}
if (open_device(tr, dev) < 0) {
fprintf(stderr, "rf2500: failed to open RF2500 device\n");
printc_err("rf2500: failed to open RF2500 device\n");
return NULL;
}

View File

@ -57,18 +57,18 @@ static int isearch_opcode(const char *term, char **arg,
int opc;
if (q->flags & ISEARCH_OPCODE) {
fprintf(stderr, "isearch: opcode already specified\n");
printc_err("isearch: opcode already specified\n");
return -1;
}
if (!opname) {
fprintf(stderr, "isearch: opcode name expected\n");
printc_err("isearch: opcode name expected\n");
return -1;
}
opc = dis_opcode_from_name(opname);
if (opc < 0) {
fprintf(stderr, "isearch: unknown opcode: %s\n", opname);
printc_err("isearch: unknown opcode: %s\n", opname);
return -1;
}
q->insn.op = opc;
@ -81,7 +81,7 @@ static int isearch_bw(const char *term, char **arg,
struct isearch_query *q)
{
if (q->flags & ISEARCH_DSIZE) {
fprintf(stderr, "isearch: operand size already specified\n");
printc_err("isearch: operand size already specified\n");
return -1;
}
@ -107,7 +107,7 @@ static int isearch_type(const char *term, char **arg,
struct isearch_query *q)
{
if (q->flags & ISEARCH_TYPE) {
fprintf(stderr, "isearch: instruction type already "
printc_err("isearch: instruction type already "
"specified\n");
return -1;
}
@ -144,13 +144,13 @@ static int isearch_addr(const char *term, char **arg,
address_t addr;
if (q->flags & which) {
fprintf(stderr, "isearch: address already specified\n");
printc_err("isearch: address already specified\n");
return -1;
}
addr_text = get_arg(arg);
if (!addr_text) {
fprintf(stderr, "isearch: address expected\n");
printc_err("isearch: address expected\n");
return -1;
}
@ -175,19 +175,19 @@ static int isearch_reg(const char *term, char **arg,
int reg;
if (q->flags & which) {
fprintf(stderr, "isearch: register already specified\n");
printc_err("isearch: register already specified\n");
return -1;
}
reg_text = get_arg(arg);
if (!reg_text) {
fprintf(stderr, "isearch: register expected\n");
printc_err("isearch: register expected\n");
return -1;
}
reg = dis_reg_from_name(reg_text);
if (reg < 0) {
fprintf(stderr, "isearch: unknown register: %s\n",
printc_err("isearch: unknown register: %s\n",
reg_text);
return -1;
}
@ -210,13 +210,13 @@ static int isearch_mode(const char *term, char **arg,
int what;
if (q->flags & which) {
fprintf(stderr, "isearch: mode already specified\n");
printc_err("isearch: mode already specified\n");
return -1;
}
what_text = get_arg(arg);
if (!what_text) {
fprintf(stderr, "isearch: mode must be specified\n");
printc_err("isearch: mode must be specified\n");
return -1;
}
@ -250,7 +250,7 @@ static int isearch_mode(const char *term, char **arg,
break;
default:
fprintf(stderr, "isearch: unknown address mode: %s\n",
printc_err("isearch: unknown address mode: %s\n",
what_text);
return -1;
}
@ -348,13 +348,13 @@ static int do_isearch(address_t addr, address_t len,
mbuf = malloc(len);
if (!mbuf) {
fprintf(stderr, "isearch: couldn't allocate memory: %s\n",
printc_err("isearch: couldn't allocate memory: %s\n",
strerror(errno));
return -1;
}
if (device_default->readmem(device_default, addr, mbuf, len) < 0) {
fprintf(stderr, "isearch: couldn't read device memory\n");
printc_err("isearch: couldn't read device memory\n");
free(mbuf);
return -1;
}
@ -405,7 +405,7 @@ int cmd_isearch(char **arg)
len_text = get_arg(arg);
if (!(addr_text && len_text)) {
fprintf(stderr, "isearch: address and length expected\n");
printc_err("isearch: address and length expected\n");
return -1;
}
@ -430,7 +430,7 @@ int cmd_isearch(char **arg)
}
if (!q.flags) {
fprintf(stderr, "isearch: no query terms given "
printc_err("isearch: no query terms given "
"(perhaps you mean \"dis\"?)\n");
return -1;
}
@ -812,7 +812,7 @@ static void cgraph_summary(struct call_graph *graph)
o)
name[0] = 0;
printf("0x%04x [%3d ==> %3d] %s\n",
printc("0x%04x [%3d ==> %3d] %s\n",
n->offset, to_count, from_count, name);
}
}
@ -831,7 +831,7 @@ static void cgraph_func_info(struct call_graph *graph, address_t addr)
i++;
if (i >= graph->node_list.size ||
CG_NODE(graph, i)->offset > addr) {
printf("No information for address 0x%04x\n", addr);
printc("No information for address 0x%04x\n", addr);
return;
}
@ -847,15 +847,15 @@ static void cgraph_func_info(struct call_graph *graph, address_t addr)
if (stab_nearest(stab_default, n->offset,
name, sizeof(name), &offset))
printf("0x%04x:\n", n->offset);
printc("0x%04x:\n", n->offset);
else if (offset)
printf("0x%04x %s+0x%x:\n", n->offset, name, offset);
printc("0x%04x %s+0x%x:\n", n->offset, name, offset);
else
printf("0x%04x %s:\n", n->offset, name);
printc("0x%04x %s:\n", n->offset, name);
if (j < graph->edge_from.size &&
CG_EDGE_FROM(graph, j)->src == n->offset) {
printf(" Callees:\n");
printc(" Callees:\n");
while (j < graph->edge_from.size) {
struct cg_edge *e = CG_EDGE_FROM(graph, j);
@ -867,17 +867,17 @@ static void cgraph_func_info(struct call_graph *graph, address_t addr)
&offset) ||
offset)
snprintf(name, sizeof(name), "0x%04x", e->dst);
printf(" %s%s\n",
printc(" %s%s\n",
e->is_tail_call ? "*" : "", name);
j++;
}
printf("\n");
printc("\n");
}
if (k < graph->edge_to.size &&
CG_EDGE_TO(graph, k)->dst == n->offset) {
printf(" Callers:\n");
printc(" Callers:\n");
while (k < graph->edge_to.size) {
struct cg_edge *e = CG_EDGE_TO(graph, k);
@ -889,7 +889,7 @@ static void cgraph_func_info(struct call_graph *graph, address_t addr)
&offset) ||
offset)
snprintf(name, sizeof(name), "0x%04x", e->src);
printf(" %s%s\n",
printc(" %s%s\n",
e->is_tail_call ? "*" : "", name);
k++;
@ -910,45 +910,45 @@ int cmd_cgraph(char **arg)
addr_text = get_arg(arg);
if (!(offset_text && len_text)) {
fprintf(stderr, "cgraph: offset and length must be "
printc_err("cgraph: offset and length must be "
"specified\n");
return -1;
}
if (expr_eval(stab_default, offset_text, &offset) < 0) {
fprintf(stderr, "cgraph: invalid offset: %s\n", offset_text);
printc_err("cgraph: invalid offset: %s\n", offset_text);
return -1;
}
offset &= ~1;
if (expr_eval(stab_default, len_text, &len) < 0) {
fprintf(stderr, "cgraph: invalid length: %s\n", len_text);
printc_err("cgraph: invalid length: %s\n", len_text);
return -1;
}
len &= ~1;
if (addr_text && expr_eval(stab_default, addr_text, &addr) < 0) {
fprintf(stderr, "cgraph: invalid address: %s\n", addr_text);
printc_err("cgraph: invalid address: %s\n", addr_text);
return -1;
}
/* Grab the memory to be analysed */
memory = malloc(len);
if (!memory) {
fprintf(stderr, "cgraph: couldn't allocate memory: %s\n",
printc_err("cgraph: couldn't allocate memory: %s\n",
strerror(errno));
return -1;
}
if (device_default->readmem(device_default, offset, memory, len) < 0) {
fprintf(stderr, "cgraph: couldn't fetch memory\n");
printc_err("cgraph: couldn't fetch memory\n");
free(memory);
return -1;
}
/* Produce and display the call graph */
if (cgraph_init(offset, len, memory, &graph) < 0) {
fprintf(stderr, "cgraph: couldn't build call graph\n");
printc_err("cgraph: couldn't build call graph\n");
free(memory);
return -1;
}

15
sim.c
View File

@ -23,6 +23,7 @@
#include "device.h"
#include "dis.h"
#include "util.h"
#include "output.h"
#include "sim.h"
#define MEM_SIZE 65536
@ -256,7 +257,7 @@ static int step_double(struct sim_device *dev, uint16_t ins)
break;
default:
fprintf(stderr, "sim: invalid double-operand opcode: "
printc_err("sim: invalid double-operand opcode: "
"0x%04x (PC = 0x%04x)\n",
opcode, dev->current_insn);
return -1;
@ -343,7 +344,7 @@ static int step_single(struct sim_device *dev, uint16_t ins)
break;
default:
fprintf(stderr, "sim: unknown single-operand opcode: 0x%04x "
printc_err("sim: unknown single-operand opcode: 0x%04x "
"(PC = 0x%04x)\n", opcode, dev->current_insn);
return -1;
}
@ -447,7 +448,7 @@ static int sim_readmem(device_t dev_base, address_t addr,
if (addr > MEM_SIZE || (addr + len) < addr ||
(addr + len) > MEM_SIZE) {
fprintf(stderr, "sim: memory read out of range\n");
printc_err("sim: memory read out of range\n");
return -1;
}
@ -465,7 +466,7 @@ static int sim_writemem(device_t dev_base, address_t addr,
if (addr > MEM_SIZE || (addr + len) < addr ||
(addr + len) > MEM_SIZE) {
fprintf(stderr, "sim: memory write out of range\n");
printc_err("sim: memory write out of range\n");
return -1;
}
@ -546,7 +547,7 @@ static device_status_t sim_poll(device_t dev_base)
}
if (dev->regs[MSP430_REG_SR] & MSP430_SR_CPUOFF) {
printf("CPU disabled\n");
printc("CPU disabled\n");
dev->running = 0;
return DEVICE_STATUS_HALTED;
}
@ -572,7 +573,7 @@ device_t sim_open(sim_fetch_func_t fetch_func,
struct sim_device *dev = malloc(sizeof(*dev));
if (!dev) {
perror("can't allocate memory for simulation");
pr_error("can't allocate memory for simulation");
return NULL;
}
@ -597,6 +598,6 @@ device_t sim_open(sim_fetch_func_t fetch_func,
dev->running = 0;
dev->current_insn = 0;
printf("Simulation started, 0x%x bytes of RAM\n", MEM_SIZE);
printc("Simulation started, 0x%x bytes of RAM\n", MEM_SIZE);
return (device_t)dev;
}

19
srec.c
View File

@ -20,6 +20,7 @@
#include <stdlib.h>
#include "srec.h"
#include "util.h"
#include "output.h"
int srec_check(FILE *in)
{
@ -69,13 +70,13 @@ int srec_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
lno++;
if (buf[0] != 'S') {
fprintf(stderr, "srec: garbage on line %d\n", lno);
printc_err("srec: garbage on line %d\n", lno);
return -1;
}
for (i = 2; ishex(buf[i]) && ishex(buf[i + 1]); i += 2) {
if (count >= sizeof(bytes)) {
fprintf(stderr, "srec: too many bytes on "
printc_err("srec: too many bytes on "
"line %d\n", lno);
return -1;
}
@ -86,7 +87,7 @@ int srec_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
while (buf[i]) {
if (!isspace(buf[i])) {
fprintf(stderr, "srec: trailing garbage on "
printc_err("srec: trailing garbage on "
"line %d\n", lno);
return -1;
}
@ -95,13 +96,13 @@ int srec_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
}
if (count < 2) {
fprintf(stderr, "srec: too few bytes on line %d\n",
printc_err("srec: too few bytes on line %d\n",
lno);
return -1;
}
if (bytes[0] + 1 != count) {
fprintf(stderr, "srec: byte count mismatch on "
printc_err("srec: byte count mismatch on "
"line %d\n", lno);
return -1;
}
@ -110,7 +111,7 @@ int srec_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
cksum += bytes[i];
cksum = ~cksum;
if (cksum != bytes[count - 1]) {
fprintf(stderr, "srec: checksum error on line %d "
printc_err("srec: checksum error on line %d "
"(calc = 0x%02x, read = 0x%02x)\n",
lno, cksum, bytes[count - 1]);
return -1;
@ -124,20 +125,20 @@ int srec_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
addr = (addr << 8) | bytes[i + 1];
if (count < addrbytes + 2) {
fprintf(stderr, "srec: too few address bytes "
printc_err("srec: too few address bytes "
"on line %d\n", lno);
return -1;
}
if (addr > 0xffff) {
fprintf(stderr, "srec: address out of range "
printc_err("srec: address out of range "
"on line %d: 0x%x\n", lno, addr);
return -1;
}
if (cb(user_data, addr, bytes + addrbytes + 1,
count - 2 - addrbytes) < 0) {
fprintf(stderr, "srec: error on line %d\n",
printc_err("srec: error on line %d\n",
lno);
return -1;
}

9
stab.c
View File

@ -27,6 +27,7 @@
#include "btree.h"
#include "stab.h"
#include "util.h"
#include "output.h"
stab_t stab_default;
@ -147,7 +148,7 @@ int stab_set(stab_t st, const char *name, int value)
addr_key_init(&akey, addr, name);
if (btree_put(st->addr, &akey, NULL) < 0 ||
btree_put(st->sym, &skey, &addr) < 0) {
fprintf(stderr, "stab: can't set %s = 0x%04x\n", name, addr);
printc_err("stab: can't set %s = 0x%04x\n", name, addr);
return -1;
}
@ -227,20 +228,20 @@ stab_t stab_new(void)
stab_t st = malloc(sizeof(*st));
if (!st) {
perror("stab: failed to allocate memory\n");
pr_error("stab: failed to allocate memory\n");
return NULL;
}
st->sym = btree_alloc(&sym_table_def);
if (!st->sym) {
fprintf(stderr, "stab: failed to allocate symbol table\n");
printc_err("stab: failed to allocate symbol table\n");
free(st);
return NULL;
}
st->addr = btree_alloc(&addr_table_def);
if (!st->addr) {
fprintf(stderr, "stab: failed to allocate address table\n");
printc_err("stab: failed to allocate address table\n");
btree_free(st->sym);
free(st);
return NULL;

View File

@ -56,7 +56,7 @@ static void namelist_print(struct vector *v)
for (i = 0; i < rows; i++) {
int j;
printf(" ");
printc(" ");
for (j = 0; j < cols; j++) {
int k = j * rows + i;
const char *text;
@ -65,12 +65,12 @@ static void namelist_print(struct vector *v)
break;
text = VECTOR_AT(*v, k, const char *);
printf("%s", text);
printc("%s", text);
for (k = strlen(text); k < max_len; k++)
printf(" ");
printc(" ");
}
printf("\n");
printc("\n");
}
}
@ -121,7 +121,7 @@ int cmd_help(char **arg)
return 0;
}
fprintf(stderr, "help: unknown command: %s\n", topic);
printc_err("help: unknown command: %s\n", topic);
return -1;
} else {
struct vector v;
@ -129,27 +129,27 @@ int cmd_help(char **arg)
vector_init(&v, sizeof(const char *));
if (!cmddb_enum(push_command_name, &v)) {
printf("Available commands:\n");
printc("Available commands:\n");
namelist_print(&v);
printf("\n");
printc("\n");
} else {
perror("help: can't allocate memory for command list");
pr_error("help: can't allocate memory for command list");
}
vector_realloc(&v, 0);
if (!opdb_enum(push_option_name, &v)) {
printf("Available options:\n");
printc("Available options:\n");
namelist_print(&v);
printf("\n");
printc("\n");
} else {
perror("help: can't allocate memory for option list");
pr_error("help: can't allocate memory for option list");
}
vector_destroy(&v);
printf("Type \"help <topic>\" for more information.\n");
printf("Press Ctrl+D to quit.\n");
printc("Type \"help <topic>\" for more information.\n");
printc("Press Ctrl+D to quit.\n");
}
return 0;
@ -180,23 +180,23 @@ static int parse_option(opdb_type_t type, union opdb_value *value,
static int display_option(void *user_data, const struct opdb_key *key,
const union opdb_value *value)
{
printf("%32s = ", key->name);
printc("%32s = ", key->name);
switch (key->type) {
case OPDB_TYPE_BOOLEAN:
printf("%s", value->boolean ? "true" : "false");
printc("%s", value->boolean ? "true" : "false");
break;
case OPDB_TYPE_NUMERIC:
printf("0x%x (%u)", value->numeric, value->numeric);
printc("0x%x (%u)", value->numeric, value->numeric);
break;
case OPDB_TYPE_STRING:
printf("%s", value->string);
printc("%s", value->string);
break;
}
printf("\n");
printc("\n");
return 0;
}
@ -208,7 +208,7 @@ int cmd_opt(char **arg)
if (opt_text) {
if (opdb_get(opt_text, &key, &value) < 0) {
fprintf(stderr, "opt: no such option: %s\n",
printc_err("opt: no such option: %s\n",
opt_text);
return -1;
}
@ -216,7 +216,7 @@ int cmd_opt(char **arg)
if (**arg) {
if (parse_option(key.type, &value, *arg) < 0) {
fprintf(stderr, "opt: can't parse option: %s\n",
printc_err("opt: can't parse option: %s\n",
*arg);
return -1;
}
@ -236,7 +236,7 @@ int cmd_read(char **arg)
char *filename = get_arg(arg);
if (!filename) {
fprintf(stderr, "read: filename must be specified\n");
printc_err("read: filename must be specified\n");
return -1;
}

53
sym.c
View File

@ -27,6 +27,7 @@
#include "expr.h"
#include "binfile.h"
#include "util.h"
#include "output.h"
#include "vector.h"
#include "sym.h"
#include "reader.h"
@ -38,17 +39,17 @@ int cmd_eval(char **arg)
char name[64];
if (expr_eval(stab_default, *arg, &addr) < 0) {
fprintf(stderr, "=: can't parse: %s\n", *arg);
printc_err("=: can't parse: %s\n", *arg);
return -1;
}
printf("0x%05x", addr);
printc("0x%05x", addr);
if (!stab_nearest(stab_default, addr, name, sizeof(name), &offset)) {
printf(" = %s", name);
printc(" = %s", name);
if (offset)
printf("+0x%x", offset);
printc("+0x%x", offset);
}
printf("\n");
printc("\n");
return 0;
}
@ -62,7 +63,7 @@ static int cmd_sym_load_add(int clear, char **arg)
in = fopen(*arg, "r");
if (!in) {
fprintf(stderr, "sym: %s: %s\n", *arg, strerror(errno));
printc_err("sym: %s: %s\n", *arg, strerror(errno));
return -1;
}
@ -87,7 +88,7 @@ static int savemap_cb(void *user_data, const char *name, address_t value)
FILE *savemap_out = (FILE *)user_data;
if (fprintf(savemap_out, "%04x t %s\n", value, name) < 0) {
perror("sym: can't write to file");
pr_error("sym: can't write to file");
return -1;
}
@ -100,13 +101,13 @@ static int cmd_sym_savemap(char **arg)
char *fname = get_arg(arg);
if (!fname) {
fprintf(stderr, "sym: filename required to save map\n");
printc_err("sym: filename required to save map\n");
return -1;
}
savemap_out = fopen(fname, "w");
if (!savemap_out) {
fprintf(stderr, "sym: couldn't write to %s: %s\n", fname,
printc_err("sym: couldn't write to %s: %s\n", fname,
strerror(errno));
return -1;
}
@ -117,7 +118,7 @@ static int cmd_sym_savemap(char **arg)
}
if (fclose(savemap_out) < 0) {
fprintf(stderr, "sym: error on close: %s\n", strerror(errno));
printc_err("sym: error on close: %s\n", strerror(errno));
return -1;
}
@ -127,7 +128,7 @@ static int cmd_sym_savemap(char **arg)
static int print_sym(void *user_data, const char *name, address_t value)
{
printf("0x%04x: %s\n", value, name);
printc("0x%04x: %s\n", value, name);
return 0;
}
@ -136,7 +137,7 @@ static int find_sym(void *user_data, const char *name, address_t value)
regex_t *find_preg = (regex_t *)user_data;
if (!regexec(find_preg, name, 0, NULL, 0))
printf("0x%04x: %s\n", value, name);
printc("0x%04x: %s\n", value, name);
return 0;
}
@ -152,7 +153,7 @@ static int cmd_sym_find(char **arg)
}
if (regcomp(&find_preg, expr, REG_EXTENDED | REG_NOSUB)) {
fprintf(stderr, "sym: failed to compile: %s\n", expr);
printc_err("sym: failed to compile: %s\n", expr);
return -1;
}
@ -191,16 +192,16 @@ static int renames_do(struct rename_data *rename, const char *replace)
sizeof(new_name) - len,
"%s%s", replace, r->old_name + r->end);
printf("%s -> %s\n", r->old_name, new_name);
printc("%s -> %s\n", r->old_name, new_name);
if (stab_get(stab_default, r->old_name, &value) < 0) {
fprintf(stderr, "sym: warning: "
printc_err("sym: warning: "
"symbol missing: %s\n",
r->old_name);
} else {
stab_del(stab_default, r->old_name);
if (stab_set(stab_default, new_name, value) < 0) {
fprintf(stderr, "sym: warning: "
printc_err("sym: warning: "
"failed to set new name: %s\n",
new_name);
}
@ -209,7 +210,7 @@ static int renames_do(struct rename_data *rename, const char *replace)
count++;
}
printf("%d symbols renamed\n", count);
printc("%d symbols renamed\n", count);
return 0;
}
@ -241,19 +242,19 @@ static int cmd_sym_rename(char **arg)
struct rename_data rename;
if (!(expr && replace)) {
fprintf(stderr, "sym: expected pattern and replacement\n");
printc_err("sym: expected pattern and replacement\n");
return -1;
}
if (regcomp(&rename.preg, expr, REG_EXTENDED)) {
fprintf(stderr, "sym: failed to compile: %s\n", expr);
printc_err("sym: failed to compile: %s\n", expr);
return -1;
}
vector_init(&rename.list, sizeof(struct rename_record));
if (stab_enum(stab_default, find_renames, &rename) < 0) {
fprintf(stderr, "sym: rename failed\n");
printc_err("sym: rename failed\n");
regfree(&rename.preg);
vector_destroy(&rename.list);
return -1;
@ -274,13 +275,13 @@ static int cmd_sym_del(char **arg)
char *name = get_arg(arg);
if (!name) {
fprintf(stderr, "sym: need a name to delete "
printc_err("sym: need a name to delete "
"symbol table entries\n");
return -1;
}
if (stab_del(stab_default, name) < 0) {
fprintf(stderr, "sym: can't delete nonexistent symbol: %s\n",
printc_err("sym: can't delete nonexistent symbol: %s\n",
name);
return -1;
}
@ -294,7 +295,7 @@ int cmd_sym(char **arg)
char *subcmd = get_arg(arg);
if (!subcmd) {
fprintf(stderr, "sym: need to specify a subcommand "
printc_err("sym: need to specify a subcommand "
"(try \"help sym\")\n");
return -1;
}
@ -313,13 +314,13 @@ int cmd_sym(char **arg)
address_t value;
if (!(name && val_text)) {
fprintf(stderr, "sym: need a name and value to set "
printc_err("sym: need a name and value to set "
"symbol table entries\n");
return -1;
}
if (expr_eval(stab_default, val_text, &value) < 0) {
fprintf(stderr, "sym: can't parse value: %s\n",
printc_err("sym: can't parse value: %s\n",
val_text);
return -1;
}
@ -344,6 +345,6 @@ int cmd_sym(char **arg)
if (!strcasecmp(subcmd, "find"))
return cmd_sym_find(arg);
fprintf(stderr, "sym: unknown subcommand: %s\n", subcmd);
printc_err("sym: unknown subcommand: %s\n", subcmd);
return -1;
}

View File

@ -20,6 +20,7 @@
#include <stdlib.h>
#include "titext.h"
#include "util.h"
#include "output.h"
static int is_address_line(const char *text)
{
@ -95,13 +96,13 @@ static int process_data_line(address_t address, const char *buf,
} else if (c >= 'a' && c <= 'f') {
x = c - 'a' + 10;
} else {
fprintf(stderr, "titext: unexpected "
printc_err("titext: unexpected "
"character: %c\n", c);
return -1;
}
if (vc >= 2) {
fprintf(stderr, "titext: too many digits "
printc_err("titext: too many digits "
"in hex value\n");
return -1;
}
@ -123,7 +124,7 @@ static int process_data_line(address_t address, const char *buf,
return data_len;
too_long:
fprintf(stderr, "titext: too many data bytes\n");
printc_err("titext: too many data bytes\n");
return -1;
}
@ -143,7 +144,7 @@ int titext_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
int count = process_data_line(address, buf,
cb, user_data);
if (count < 0) {
fprintf(stderr, "titext: data error on line "
printc_err("titext: data error on line "
"%d\n", lno);
return -1;
}

11
uif.c
View File

@ -27,6 +27,7 @@
#include "uif.h"
#include "util.h"
#include "output.h"
#ifdef __APPLE__
#define B460800 460800
@ -48,7 +49,7 @@ static int serial_send(transport_t tr_base, const uint8_t *data, int len)
#endif
if (write_all(tr->serial_fd, data, len) < 0) {
perror("uif: write error");
pr_error("uif: write error");
return -1;
}
@ -62,7 +63,7 @@ static int serial_recv(transport_t tr_base, uint8_t *data, int max_len)
r = read_with_timeout(tr->serial_fd, data, max_len);
if (r < 0) {
perror("uif: read error");
pr_error("uif: read error");
return -1;
}
@ -85,7 +86,7 @@ transport_t uif_open(const char *device, int is_olimex)
struct uif_transport *tr = malloc(sizeof(*tr));
if (!tr) {
perror("uif: couldn't allocate memory");
pr_error("uif: couldn't allocate memory");
return NULL;
}
@ -93,11 +94,11 @@ transport_t uif_open(const char *device, int is_olimex)
tr->base.recv = serial_recv;
tr->base.destroy = serial_destroy;
printf("Trying to open UIF on %s...\n", device);
printc("Trying to open UIF on %s...\n", device);
tr->serial_fd = open_serial(device, is_olimex ? B500000 : B460800);
if (tr->serial_fd < 0) {
fprintf(stderr, "uif: can't open serial device: %s: %s\n",
printc_err("uif: can't open serial device: %s: %s\n",
device, strerror(errno));
free(tr);
return NULL;

View File

@ -20,6 +20,7 @@
#include <string.h>
#include "usbutil.h"
#include "util.h"
#include "output.h"
static const char *device_help(const struct usb_device *dev)
{
@ -49,12 +50,12 @@ void usbutil_list(void)
const struct usb_device *dev;
int busnum = atoi(bus->dirname);
printf("Devices on bus %03d:\n", busnum);
printc("Devices on bus %03d:\n", busnum);
for (dev = bus->devices; dev; dev = dev->next) {
int devnum = atoi(dev->filename);
printf(" %03d:%03d %04x:%04x %s\n",
printc(" %03d:%03d %04x:%04x %s\n",
busnum, devnum,
dev->descriptor.idVendor,
dev->descriptor.idProduct,
@ -76,7 +77,7 @@ struct usb_device *usbutil_find_by_id(int vendor, int product)
return dev;
}
fprintf(stderr, "usbutil: unable to find a device matching "
printc_err("usbutil: unable to find a device matching "
"%04x:%04x\n", vendor, product);
return NULL;
@ -98,7 +99,7 @@ struct usb_device *usbutil_find_by_loc(const char *loc)
dev_text = strtok(NULL, ":\t\r\n");
if (!(bus_text && dev_text)) {
fprintf(stderr, "usbutil: location must be specified as "
printc_err("usbutil: location must be specified as "
"<bus>:<device>\n");
return NULL;
}
@ -121,7 +122,7 @@ struct usb_device *usbutil_find_by_loc(const char *loc)
}
}
fprintf(stderr, "usbutil: unable to find %03d:%03d\n",
printc_err("usbutil: unable to find %03d:%03d\n",
target_bus, target_dev);
return NULL;
}

9
util.c
View File

@ -30,6 +30,7 @@
#include <assert.h>
#include "util.h"
#include "output.h"
static volatile int ctrlc_flag;
@ -237,14 +238,14 @@ void debug_hexdump(const char *label, const uint8_t *data, int len)
{
int offset = 0;
printf("%s [0x%x bytes]\n", label, len);
printc("%s [0x%x bytes]\n", label, len);
while (offset < len) {
int i;
printf(" ");
printc(" ");
for (i = 0; i < 16 && offset + i < len; i++)
printf("%02x ", data[offset + i]);
printf("\n");
printc("%02x ", data[offset + i]);
printc("\n");
offset += i;
}