Skip to content

Commit 86fc749

Browse files
author
Clément Léger
committed
ihex2cyacd: fix hex parsing
1 parent b98a77b commit 86fc749

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

ihex2cyacd/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ LFLAGS :=
1212
all: ihex2cyacd
1313

1414
$(BUILD_DIR)/ihex2cyacd_cmdline.c: $(SRC_DIR)/ihex2cyacd.ggo
15+
@mkdir -p $(dir $@)
1516
gengetopt -i $< -F ihex2cyacd_cmdline --output-dir=$(BUILD_DIR)/ --func-name=ihex2cyacd_cmdline_parser -a ihex2cyacd_args_info
1617

1718
$(BUILD_DIR)/ihex2cyacd_cmdline.h: $(BUILD_DIR)/ihex2cyacd_cmdline.c
1819

1920
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c $(BUILD_DIR)/ihex2cyacd_cmdline.h
20-
@mkdir -p $(BUILD_DIR)
21+
@mkdir -p $(dir $@)
2122
$(CC) -c -o $@ $< $(CFLAGS)
2223

2324
$(BUILD_DIR)/%.o: $(BUILD_DIR)/%.c
24-
@mkdir -p $(BUILD_DIR)
25+
@mkdir -p $(dir $@)
2526
$(CC) -c -o $@ $^ $(CFLAGS)
2627

2728
ihex2cyacd: $(OBJ_FILES) $(BUILD_DIR)/ihex2cyacd_cmdline.o

ihex2cyacd/src/ihex2cyacd.c

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,42 @@
1919
struct cyacd_header_info {
2020
uint32_t silicon_id;
2121
uint8_t silicon_rev;
22+
uint16_t flash_row_size;
2223
};
2324

2425
static struct cyacd_header_info header_infos[] =
2526
{
26-
[cpu_arg_CY8C41] = {0x04161193, 0x11},
27-
[cpu_arg_CY8C42] = {0x04C81193, 0x11},
27+
[cpu_arg_CY8C41] = {0x04161193, 0x11, 128},
28+
[cpu_arg_CY8C42] = {0x04C81193, 0x11, 128},
2829
};
2930

3031
static struct ihex2cyacd_args_info args_info;
3132

3233
#define get_line_chars(__str, __out_str, __n) \
3334
__out_str[__n] = 0; \
34-
memcpy(__out_str, __str, __n); \
35+
strncpy(__out_str, __str, __n); \
3536
__str += __n;
3637

3738
static int parse_ihex_line(const char *line, uint8_t *length, uint16_t *addr, uint8_t *type, uint8_t data[MAX_IHEX_FILE_LENGTH])
3839
{
3940
char tmp_buf[MAX_IHEX_FILE_LENGTH];
4041
int i;
4142
uint8_t crc, sum = 0;
42-
43+
44+
if(line[0] != ':') {
45+
printf("Missing field start\n");
46+
return 1;
47+
}
48+
line++;
49+
4350
get_line_chars(line, tmp_buf, 2);
4451
*length = strtol(tmp_buf, NULL, 16);
4552
sum += *length;
4653

4754
get_line_chars(line, tmp_buf, 4);
4855
*addr = strtol(tmp_buf, NULL, 16);
49-
sum += ((uint8_t) (*addr & 0xFF));
50-
sum += ((uint8_t) ((*addr) >> 8 & 0xFF));
56+
sum += ((uint8_t) ((*addr) & 0xFF));
57+
sum += ((uint8_t) (((*addr) >> 8) & 0xFF));
5158

5259
get_line_chars(line, tmp_buf, 2);
5360
*type = strtol(tmp_buf, NULL, 16);
@@ -61,8 +68,9 @@ static int parse_ihex_line(const char *line, uint8_t *length, uint16_t *addr, ui
6168

6269
get_line_chars(line, tmp_buf, 2);
6370
crc = strtol(tmp_buf, NULL, 16);
71+
sum += crc;
6472

65-
if (crc != (~sum - 1)) {
73+
if (sum != 0) {
6674
printf("CRC failed\n");
6775
return 1;
6876
}
@@ -73,10 +81,13 @@ static int parse_ihex_line(const char *line, uint8_t *length, uint16_t *addr, ui
7381
int main(int argc, char **argv)
7482
{
7583
uint32_t bootloader_text_rows;
76-
char line_ptr[MAX_IHEX_FILE_LENGTH];
84+
char *line_ptr;
85+
uint8_t data[MAX_IHEX_FILE_LENGTH],length, type;
86+
uint16_t addr;
7787
FILE *input_hex, *output_cyacd;
7888
struct cyacd_header_info *infos;
7989
size_t line_length = MAX_IHEX_FILE_LENGTH;
90+
int ret;
8091

8192
if (ihex2cyacd_cmdline_parser(argc, argv, &args_info) != 0) {
8293
return EXIT_FAILURE;
@@ -92,14 +103,25 @@ int main(int argc, char **argv)
92103
printf("Failed to open output file %s\n", args_info.output_arg);
93104
return 1;
94105
}
106+
line_ptr = malloc(MAX_IHEX_FILE_LENGTH);
107+
if(!line_ptr) {
108+
printf("Failed to allocate data\n");
109+
return 1;
110+
}
95111
infos = &header_infos[args_info.cpu_arg];
96112

97-
bootloader_text_rows = args_info.bootloader_size_arg / args_info.flash_row_size_arg;
113+
bootloader_text_rows = args_info.bootloader_size_arg / infos->flash_row_size;
98114

99115
fprintf(output_cyacd, "%08x%02x00\r\n", infos->silicon_id, infos->silicon_rev);
100116

101-
while( getline(&line_ptr, &line_length, input_hex)) {
102-
117+
while( getline(&line_ptr, &line_length, input_hex) > 0) {
118+
ret = parse_ihex_line(line_ptr, &length, &addr, &type, data);
119+
if (ret) {
120+
printf("Failed to parse ihex file\n");
121+
return 1;
122+
}
123+
line_length = MAX_IHEX_FILE_LENGTH;
124+
/* TODO: concat data */
103125
}
104126

105127

ihex2cyacd/src/ihex2cyacd.ggo

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ description "ihex2cyacd is a an utility to create cyacd files"
77

88
option "input" i "Input ihex file" string required
99
option "bootloader_size" b "Bootloader text size file" int required
10-
option "flash_row_size" r "Flash row size" int required
1110
option "output" o "Output cyacd file" string required
1211
option "cpu" c "CPU type" values="CY8C41","CY8C42" enum default="CY8C42" optional
1312

0 commit comments

Comments
 (0)