From 9ecf177655013ba7efb1596b050b6d616c8024ee Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Thu, 5 Aug 2010 16:12:35 +1200 Subject: [PATCH] Binary file interface is now 32-bit clean. --- binfile.h | 2 +- devcmd.c | 4 ++-- elf32.c | 6 +++--- ihex.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++------ srec.c | 2 +- symmap.c | 2 +- titext.c | 4 ++-- 7 files changed, 59 insertions(+), 16 deletions(-) diff --git a/binfile.h b/binfile.h index c99c5bf..1810cad 100644 --- a/binfile.h +++ b/binfile.h @@ -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 diff --git a/devcmd.c b/devcmd.c index 2a9fefd..996ec54 100644 --- a/devcmd.c +++ b/devcmd.c @@ -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; diff --git a/elf32.c b/elf32.c index d7e8b9c..35755de 100644 --- a/elf32.c +++ b/elf32.c @@ -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"); diff --git a/ihex.c b/ihex.c index 8ba3593..4a8aea4 100644 --- a/ihex.c +++ b/ihex.c @@ -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; } diff --git a/srec.c b/srec.c index cb43c2a..3a9b100 100644 --- a/srec.c +++ b/srec.c @@ -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]; diff --git a/symmap.c b/symmap.c index ef5b740..1c97a58 100644 --- a/symmap.c +++ b/symmap.c @@ -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; diff --git a/titext.c b/titext.c index 73f7992..cf8b721 100644 --- a/titext.c +++ b/titext.c @@ -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];