2010-07-09 02:28:27 +00:00
|
|
|
/* MSPDebug - debugging tool for MSP430 MCUs
|
|
|
|
* 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 <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "titext.h"
|
2010-07-12 04:36:01 +00:00
|
|
|
#include "util.h"
|
2010-08-16 23:07:03 +00:00
|
|
|
#include "output.h"
|
2010-07-09 02:28:27 +00:00
|
|
|
|
|
|
|
static int is_address_line(const char *text)
|
|
|
|
{
|
|
|
|
if (*text != '@')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
text++;
|
|
|
|
if (!*text || isspace(*text))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (*text && !isspace(*text)) {
|
|
|
|
if (!ishex(*text))
|
|
|
|
return 0;
|
|
|
|
text++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*text) {
|
|
|
|
if (!isspace(*text))
|
|
|
|
return 0;
|
|
|
|
text++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int is_data_line(const char *text)
|
|
|
|
{
|
|
|
|
while (*text) {
|
|
|
|
if (!(ishex(*text) || isspace(*text)))
|
|
|
|
return 0;
|
|
|
|
text++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int titext_check(FILE *in)
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
|
|
|
|
rewind(in);
|
2010-07-12 04:36:01 +00:00
|
|
|
if (!fgets(buf, sizeof(buf), in))
|
|
|
|
return 0;
|
|
|
|
|
2010-07-09 02:28:27 +00:00
|
|
|
return is_address_line(buf);
|
|
|
|
}
|
|
|
|
|
2010-08-05 04:12:35 +00:00
|
|
|
static int process_data_line(address_t address, const char *buf,
|
2010-07-09 02:28:27 +00:00
|
|
|
binfile_imgcb_t cb, void *user_data)
|
|
|
|
{
|
|
|
|
uint8_t data[64];
|
|
|
|
int data_len = 0;
|
|
|
|
int value = 0;
|
|
|
|
int vc = 0;
|
|
|
|
|
|
|
|
while (*buf) {
|
|
|
|
int c = *(buf++);
|
|
|
|
int x;
|
|
|
|
|
|
|
|
if (isspace(c)) {
|
|
|
|
if (vc) {
|
|
|
|
if (data_len >= sizeof(data))
|
|
|
|
goto too_long;
|
|
|
|
data[data_len++] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
vc = 0;
|
|
|
|
} else {
|
|
|
|
if (isdigit(c)) {
|
|
|
|
x = c - '0';
|
|
|
|
} else if (c >= 'A' && c <= 'F') {
|
|
|
|
x = c - 'A' + 10;
|
|
|
|
} else if (c >= 'a' && c <= 'f') {
|
|
|
|
x = c - 'a' + 10;
|
|
|
|
} else {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("titext: unexpected "
|
2010-07-09 02:28:27 +00:00
|
|
|
"character: %c\n", c);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vc >= 2) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("titext: too many digits "
|
2010-07-09 02:28:27 +00:00
|
|
|
"in hex value\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = (value << 4) | x;
|
|
|
|
vc++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vc) {
|
|
|
|
if (data_len >= sizeof(data))
|
|
|
|
goto too_long;
|
|
|
|
data[data_len++] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cb(user_data, address, data, data_len) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return data_len;
|
|
|
|
|
|
|
|
too_long:
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("titext: too many data bytes\n");
|
2010-07-09 02:28:27 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int titext_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
|
|
|
|
{
|
2010-08-05 04:12:35 +00:00
|
|
|
address_t address = 0;
|
2010-07-09 02:28:27 +00:00
|
|
|
int lno = 0;
|
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
rewind(in);
|
|
|
|
while (fgets(buf, sizeof(buf), in)) {
|
|
|
|
lno++;
|
|
|
|
|
|
|
|
if (is_address_line(buf)) {
|
|
|
|
address = strtoul(buf + 1, NULL, 16);
|
|
|
|
} else if (is_data_line(buf)) {
|
|
|
|
int count = process_data_line(address, buf,
|
|
|
|
cb, user_data);
|
|
|
|
if (count < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("titext: data error on line "
|
2010-07-09 02:28:27 +00:00
|
|
|
"%d\n", lno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
address += count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|