|
|
@ -22,25 +22,21 @@ |
|
|
|
*/ |
|
|
|
|
|
|
|
#include "kk_ihex_read.h" |
|
|
|
#include "libfvhex2bin.h" |
|
|
|
#include <stdbool.h> |
|
|
|
#include <stdio.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <string.h> |
|
|
|
|
|
|
|
#define AUTODETECT_ADDRESS (~0UL) |
|
|
|
|
|
|
|
|
|
|
|
static unsigned long line_number = 1L; |
|
|
|
static unsigned long file_position = 0L; |
|
|
|
static unsigned long address_offset = 0UL; |
|
|
|
static bool debug_enabled = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static FILE *infile = NULL; |
|
|
|
static FILE *outfile = NULL; |
|
|
|
|
|
|
|
int hex2bin(const char *infilename, const char *outfilename) |
|
|
|
int heckfv_hex2bin(char *infilename, char *outfilename) |
|
|
|
{ |
|
|
|
int ret = 0; |
|
|
|
if (!(infile = fopen(infilename, "r"))) { |
|
|
@ -55,9 +51,7 @@ int hex2bin(const char *infilename, const char *outfilename) |
|
|
|
ihex_count_t count; |
|
|
|
char buf[256]; |
|
|
|
|
|
|
|
ihex_read_at_address( |
|
|
|
&ihex, |
|
|
|
(address_offset != AUTODETECT_ADDRESS) ? (ihex_address_t)address_offset : 0); |
|
|
|
ihex_read_at_address(&ihex, (ihex_address_t)address_offset); |
|
|
|
while (fgets(buf, sizeof(buf), infile)) { |
|
|
|
count = (ihex_count_t)strlen(buf); |
|
|
|
ihex_read_bytes(&ihex, buf, count); |
|
|
@ -87,28 +81,8 @@ ihex_bool_t ihex_data_read(struct ihex_state *ihex, ihex_record_type_t type, ihe |
|
|
|
} |
|
|
|
if (type == IHEX_DATA_RECORD) { |
|
|
|
unsigned long address = (unsigned long)IHEX_LINEAR_ADDRESS(ihex); |
|
|
|
if (address < address_offset) { |
|
|
|
if (address_offset == AUTODETECT_ADDRESS) { |
|
|
|
// autodetect initial address
|
|
|
|
address_offset = address; |
|
|
|
if (debug_enabled) { |
|
|
|
(void)fprintf(stderr, "Address offset: 0x%lx\n", address_offset); |
|
|
|
} |
|
|
|
} else { |
|
|
|
(void)fprintf(stderr, "Address underflow on line %lu\n", line_number); |
|
|
|
exit(EXIT_FAILURE); |
|
|
|
} |
|
|
|
} |
|
|
|
address -= address_offset; |
|
|
|
if (address != file_position) { |
|
|
|
if (debug_enabled) { |
|
|
|
(void)fprintf( |
|
|
|
stderr, |
|
|
|
"Seeking from 0x%lx to 0x%lx on line %lu\n", |
|
|
|
file_position, |
|
|
|
address, |
|
|
|
line_number); |
|
|
|
} |
|
|
|
if (outfile == stdout || fseek(outfile, (long)address, SEEK_SET)) { |
|
|
|
if (file_position < address) { |
|
|
|
// "seek" forward in stdout by writing NUL bytes
|
|
|
@ -128,9 +102,6 @@ ihex_bool_t ihex_data_read(struct ihex_state *ihex, ihex_record_type_t type, ihe |
|
|
|
} |
|
|
|
file_position += ihex->length; |
|
|
|
} else if (type == IHEX_END_OF_FILE_RECORD) { |
|
|
|
if (debug_enabled) { |
|
|
|
(void)fprintf(stderr, "%lu bytes written\n", file_position); |
|
|
|
} |
|
|
|
if (outfile != stdout) { |
|
|
|
(void)fclose(outfile); |
|
|
|
} |
|
|
|