19
19
struct cyacd_header_info {
20
20
uint32_t silicon_id ;
21
21
uint8_t silicon_rev ;
22
+ uint16_t flash_row_size ;
22
23
};
23
24
24
25
static struct cyacd_header_info header_infos [] =
25
26
{
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 },
28
29
};
29
30
30
31
static struct ihex2cyacd_args_info args_info ;
31
32
32
33
#define get_line_chars (__str , __out_str , __n ) \
33
34
__out_str[__n] = 0; \
34
- memcpy (__out_str, __str, __n); \
35
+ strncpy (__out_str, __str, __n); \
35
36
__str += __n;
36
37
37
38
static int parse_ihex_line (const char * line , uint8_t * length , uint16_t * addr , uint8_t * type , uint8_t data [MAX_IHEX_FILE_LENGTH ])
38
39
{
39
40
char tmp_buf [MAX_IHEX_FILE_LENGTH ];
40
41
int i ;
41
42
uint8_t crc , sum = 0 ;
42
-
43
+
44
+ if (line [0 ] != ':' ) {
45
+ printf ("Missing field start\n" );
46
+ return 1 ;
47
+ }
48
+ line ++ ;
49
+
43
50
get_line_chars (line , tmp_buf , 2 );
44
51
* length = strtol (tmp_buf , NULL , 16 );
45
52
sum += * length ;
46
53
47
54
get_line_chars (line , tmp_buf , 4 );
48
55
* 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 ));
51
58
52
59
get_line_chars (line , tmp_buf , 2 );
53
60
* 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
61
68
62
69
get_line_chars (line , tmp_buf , 2 );
63
70
crc = strtol (tmp_buf , NULL , 16 );
71
+ sum += crc ;
64
72
65
- if (crc != (~ sum - 1 ) ) {
73
+ if (sum != 0 ) {
66
74
printf ("CRC failed\n" );
67
75
return 1 ;
68
76
}
@@ -73,10 +81,13 @@ static int parse_ihex_line(const char *line, uint8_t *length, uint16_t *addr, ui
73
81
int main (int argc , char * * argv )
74
82
{
75
83
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 ;
77
87
FILE * input_hex , * output_cyacd ;
78
88
struct cyacd_header_info * infos ;
79
89
size_t line_length = MAX_IHEX_FILE_LENGTH ;
90
+ int ret ;
80
91
81
92
if (ihex2cyacd_cmdline_parser (argc , argv , & args_info ) != 0 ) {
82
93
return EXIT_FAILURE ;
@@ -92,14 +103,25 @@ int main(int argc, char **argv)
92
103
printf ("Failed to open output file %s\n" , args_info .output_arg );
93
104
return 1 ;
94
105
}
106
+ line_ptr = malloc (MAX_IHEX_FILE_LENGTH );
107
+ if (!line_ptr ) {
108
+ printf ("Failed to allocate data\n" );
109
+ return 1 ;
110
+ }
95
111
infos = & header_infos [args_info .cpu_arg ];
96
112
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 ;
98
114
99
115
fprintf (output_cyacd , "%08x%02x00\r\n" , infos -> silicon_id , infos -> silicon_rev );
100
116
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 */
103
125
}
104
126
105
127
0 commit comments