summaryrefslogtreecommitdiff
path: root/xpp/utils/hexfile.c
diff options
context:
space:
mode:
Diffstat (limited to 'xpp/utils/hexfile.c')
-rw-r--r--xpp/utils/hexfile.c73
1 files changed, 49 insertions, 24 deletions
diff --git a/xpp/utils/hexfile.c b/xpp/utils/hexfile.c
index f6e4149..a4b12c5 100644
--- a/xpp/utils/hexfile.c
+++ b/xpp/utils/hexfile.c
@@ -57,6 +57,38 @@ int checksum(struct hexline *hexline)
return chksm & 0xFF;
}
+int dump_hexline(int recordno, struct hexline *line, FILE *outfile)
+{
+ uint8_t ll;
+ uint16_t offset;
+ uint8_t tt;
+ uint8_t old_chksum;
+ uint8_t new_chksum;
+ uint8_t *data;
+ unsigned int i;
+
+ ll = line->d.content.header.ll;
+ offset = line->d.content.header.offset;
+ tt = line->d.content.header.tt;
+ fprintf(outfile, ":%02X%04X%02X", ll, offset, tt);
+ data = line->d.content.tt_data.data;
+ for(i = 0; i < ll; i++) {
+ fprintf(outfile, "%02X", data[i]);
+ }
+ old_chksum = data[ll];
+ data[ll] = 0;
+ new_chksum = 0xFF - checksum(line) + 1;
+ data[ll] = old_chksum;
+ fprintf(outfile, "%02X\n", new_chksum);
+ if(new_chksum != old_chksum) {
+ if(report_func)
+ report_func(LOG_ERR, "record #%d: new_chksum(%02X) != old_chksum(%02X)\n",
+ recordno, new_chksum, old_chksum);
+ return 0;
+ }
+ return 1;
+}
+
static int update_hexline(struct hexdata *hexdata, char *buf)
{
int ret;
@@ -142,8 +174,10 @@ static int update_hexline(struct hexdata *hexdata, char *buf)
}
hexline->d.content.header.ll--; /* Fix the checksum */
if(checksum(hexline) != 0) {
- if(report_func)
- report_func(LOG_ERR, "Bad checksum\n");
+ if(report_func) {
+ report_func(LOG_ERR, "Bad checksum (%d instead of 0)\n", checksum(hexline));
+ dump_hexline(last_line, hexline, stderr);
+ }
return -EINVAL;
}
if(hexdata->got_eof)
@@ -166,14 +200,7 @@ void free_hexdata(struct hexdata *hexdata)
int dump_hexfile(struct hexdata *hexdata, FILE *outfile)
{
- uint8_t ll;
- uint16_t offset;
- uint8_t tt;
- uint8_t old_chksum;
- uint8_t new_chksum;
- uint8_t *data;
unsigned int i;
- unsigned int j;
for(i = 0; i <= hexdata->last_line; i++) {
struct hexline *line = hexdata->lines[i];
@@ -182,20 +209,8 @@ int dump_hexfile(struct hexdata *hexdata, FILE *outfile)
report_func(LOG_ERR, "Missing line at #%d\n", i);
return -EINVAL;
}
- ll = line->d.content.header.ll;
- offset = line->d.content.header.offset;
- tt = line->d.content.header.tt;
- fprintf(outfile, ":%02X%04X%02X", ll, offset, tt);
- data = line->d.content.tt_data.data;
- for(j = 0; j < ll; j++) {
- fprintf(outfile, "%02X", data[j]);
- }
- old_chksum = data[ll];
- data[ll] = 0;
- new_chksum = 0xFF - checksum(line) + 1;
- data[ll] = old_chksum;
- assert(new_chksum == old_chksum);
- fprintf(outfile, "%02X\n", new_chksum);
+ if(!dump_hexline(i, line, outfile))
+ return -EINVAL;
}
return 0;
}
@@ -299,8 +314,18 @@ struct hexdata *parse_hexfile(const char *fname, unsigned int maxlines)
}
chomp(buf);
if(buf[0] == '#') {
+ char *dollar_start;
+ char *dollar_end;
+
if(report_func)
- report_func(LOG_INFO, "Comment '%s'\n", buf + 1);
+ report_func(LOG_INFO, "Comment: %s\n", buf + 1);
+ /* Search for RCS keywords */
+ if((dollar_start = strchr(buf, '$')) == NULL)
+ continue;
+ if((dollar_end = strchr(dollar_start + 1, '$')) == NULL)
+ continue;
+ *(dollar_end + 1) = '\0';
+ strncpy(hexdata->version_info, dollar_start, BUFSIZ - 1);
continue;
}
if(buf[0] != ':') {