Binary file interface is now 32-bit clean.

This commit is contained in:
Daniel Beer 2010-08-05 16:12:35 +12:00
parent 5b2c75563d
commit 9ecf177655
7 changed files with 59 additions and 16 deletions

View File

@ -25,7 +25,7 @@
/* Callback for binary image data */
typedef int (*binfile_imgcb_t)(void *user_data,
uint16_t addr, const uint8_t *data, int len);
address_t addr, const uint8_t *data, int len);
#define BINFILE_HAS_SYMS 0x01
#define BINFILE_HAS_TEXT 0x02

View File

@ -452,7 +452,7 @@ struct prog_data {
device_t dev;
uint8_t buf[128];
uint16_t addr;
address_t addr;
int len;
int have_erased;
};
@ -494,7 +494,7 @@ static int prog_flush(struct prog_data *prog)
}
static int prog_feed(void *user_data,
uint16_t addr, const uint8_t *data, int len)
address_t addr, const uint8_t *data, int len)
{
struct prog_data *prog = (struct prog_data *)user_data;

View File

@ -136,11 +136,11 @@ static uint32_t file_to_phys(struct elf32_info *info, uint32_t v)
}
static int feed_section(struct elf32_info *info,
FILE *in, int offset, int size, binfile_imgcb_t cb,
void *user_data)
FILE *in, uint32_t offset, uint32_t size,
binfile_imgcb_t cb, void *user_data)
{
uint8_t buf[1024];
uint16_t addr = file_to_phys(info, offset);
uint32_t addr = file_to_phys(info, offset);
if (fseek(in, offset, SEEK_SET) < 0) {
perror("elf32: can't seek to section");

55
ihex.c
View File

@ -28,12 +28,16 @@ int ihex_check(FILE *in)
}
static int feed_line(FILE *in, uint8_t *data, int nbytes, binfile_imgcb_t cb,
void *user_data)
void *user_data, address_t *segment_offset)
{
uint8_t cksum = 0;
address_t address;
uint8_t type;
uint8_t *payload;
int data_len;
int i;
if (nbytes < 5 || data[3])
if (nbytes < 5)
return 0;
/* Verify checksum */
@ -47,15 +51,53 @@ static int feed_line(FILE *in, uint8_t *data, int nbytes, binfile_imgcb_t cb,
return -1;
}
return cb(user_data,
((uint16_t)data[1]) << 8 | ((uint16_t)data[2]),
data + 4, nbytes - 5);
/* Extract other bits */
type = data[3];
address = (data[1] << 8) | data[2];
payload = data + 4;
data_len = nbytes - 5;
switch (type) {
case 0:
return cb(user_data, address, payload, data_len);
case 1:
break;
case 2:
if (data_len != 2) {
fprintf(stderr, "ihex: invalid 02 record\n");
return -1;
}
*segment_offset = (address_t)((payload[0] << 8) |
payload[1]) << 4;
break;
case 4:
if (data_len != 2) {
fprintf(stderr, "ihex: invalid 04 record\n");
return -1;
}
*segment_offset = (address_t)((payload[0] << 8) |
payload[1]) << 16;
break;
default:
fprintf(stderr, "warning: ihex: unknown record type: "
"0x%02x\n", type);
break;
}
return 0;
}
int ihex_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
{
char buf[128];
int lno = 0;
address_t segment_offset = 0;
rewind(in);
while (fgets(buf, sizeof(buf), in)) {
@ -85,7 +127,8 @@ int ihex_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
}
/* Handle the line */
if (feed_line(in, data, nbytes, cb, user_data) < 0) {
if (feed_line(in, data, nbytes, cb, user_data,
&segment_offset) < 0) {
fprintf(stderr, "ihex: error on line %d\n", lno);
return -1;
}

2
srec.c
View File

@ -118,7 +118,7 @@ int srec_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
if (buf[1] >= '1' && buf[1] <= '3') {
int addrbytes = buf[1] - '1' + 2;
int addr = 0;
address_t addr = 0;
for (i = 0; i < addrbytes; i++)
addr = (addr << 8) | bytes[i + 1];

View File

@ -58,7 +58,7 @@ int symmap_syms(FILE *in, stab_t stab)
name = strtok(NULL, " \t\r\n");
if (addr && name) {
int addr_val = strtoul(addr, NULL, 16);
address_t addr_val = strtoul(addr, NULL, 16);
if (stab_set(stab, name, addr_val) < 0)
return -1;

View File

@ -67,7 +67,7 @@ int titext_check(FILE *in)
return is_address_line(buf);
}
static int process_data_line(int address, const char *buf,
static int process_data_line(address_t address, const char *buf,
binfile_imgcb_t cb, void *user_data)
{
uint8_t data[64];
@ -129,7 +129,7 @@ static int process_data_line(int address, const char *buf,
int titext_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
{
int address = 0;
address_t address = 0;
int lno = 0;
char buf[128];