64
64
#define MXT_RESET_TIME 2
65
65
#define MXT_BOOTLOADER_DELAY 50000
66
66
67
- //******************************************************************************
68
- /// \brief File operation wrapper object
69
- struct file_context {
70
- FILE * fp ;
71
- int (* get )(struct file_context * fpc , unsigned char * ptr );
72
- int (* put )(struct file_context * fpc , unsigned char val );
73
- };
74
-
75
67
//******************************************************************************
76
68
/// \brief Bootloader context object
77
69
struct flash_context {
@@ -80,7 +72,7 @@ struct flash_context {
80
72
struct libmaxtouch_ctx * ctx ;
81
73
bool have_bootloader_version ;
82
74
bool extended_id_mode ;
83
- struct file_context fpc ;
75
+ FILE * fp ;
84
76
long file_size ;
85
77
char curr_version [MXT_FW_VER_LEN ];
86
78
int i2c_adapter ;
@@ -91,112 +83,6 @@ struct flash_context {
91
83
bool usb_bootloader ;
92
84
};
93
85
94
- //******************************************************************************
95
- /// \brief Read value from binary file
96
- static int fpc_get_value (struct file_context * fpc , unsigned char * ptr )
97
- {
98
- int val ;
99
-
100
- val = fgetc (fpc -> fp );
101
-
102
- if (feof (fpc -> fp )) return EOF ;
103
-
104
- * ptr = val ;
105
-
106
- return 0 ;
107
- }
108
-
109
- //******************************************************************************
110
- /// \brief Read hexadecimal value from file
111
- static int fpc_get_hex_value (struct file_context * fpc , unsigned char * ptr )
112
- {
113
- char str [] = "00\0" ;
114
- int val ;
115
-
116
- str [0 ] = fgetc (fpc -> fp );
117
- str [1 ] = fgetc (fpc -> fp );
118
-
119
- if (feof (fpc -> fp )) return EOF ;
120
-
121
- if (sscanf (str , "%x" , & val ) != 1 ) {
122
- return EOF ;
123
- }
124
-
125
- * ptr = val ;
126
-
127
- return 0 ;
128
- }
129
-
130
- //******************************************************************************
131
- /// \brief Read value from binary file
132
- static int fpc_put_value (struct file_context * fpc , unsigned char val )
133
- {
134
- fputc (val , fpc -> fp );
135
-
136
- return ferror (fpc -> fp );
137
- }
138
-
139
- //******************************************************************************
140
- /// \brief Read hexadecimal value from file
141
- static int fpc_put_hex_value (struct file_context * fpc , unsigned char val )
142
- {
143
- int ret ;
144
-
145
- ret = fprintf (fpc -> fp , "%02hhX" , val );
146
-
147
- if (ret != 2 ) {
148
- return ret ;
149
- }
150
-
151
- return 0 ;
152
- }
153
-
154
- //******************************************************************************
155
- /// \brief Open file in hex format
156
- static int open_hex_file (struct file_context * fpc , const char * filename , const char * mode )
157
- {
158
- fpc -> fp = fopen (filename , mode );
159
- if (!fpc -> fp ) {
160
- return errno ;
161
- }
162
-
163
- fpc -> get = fpc_get_hex_value ;
164
- fpc -> put = fpc_put_hex_value ;
165
-
166
- return 0 ;
167
- }
168
-
169
- //******************************************************************************
170
- /// \brief Open binary file
171
- static int open_bin_file (struct file_context * fpc , const char * filename , const char * mode )
172
- {
173
- fpc -> fp = fopen (filename , mode );
174
- if (!fpc -> fp ) {
175
- return errno ;
176
- }
177
-
178
- fpc -> get = fpc_get_value ;
179
- fpc -> put = fpc_put_value ;
180
-
181
- return 0 ;
182
- }
183
-
184
- //******************************************************************************
185
- /// \brief Open file in format based on filename
186
- static int open_file (struct file_context * fpc , const char * filename , const char * mode )
187
- {
188
- int ret ;
189
-
190
- char * extension = strrchr (filename , '.' );
191
-
192
- if (extension && !strcmp (extension , ".enc" ))
193
- ret = open_hex_file (fpc , filename , mode );
194
- else
195
- ret = open_bin_file (fpc , filename , mode );
196
-
197
- return ret ;
198
- }
199
-
200
86
//******************************************************************************
201
87
/// \brief Wait for CHG line to indicate bootloader state change
202
88
/// \return #mxt_rc
@@ -356,6 +242,26 @@ static int mxt_check_bootloader(struct flash_context *fw, unsigned int state)
356
242
return MXT_SUCCESS ;
357
243
}
358
244
245
+ //******************************************************************************
246
+ /// \brief Read hexadecimal value from file
247
+ static int get_hex_value (struct flash_context * fw , unsigned char * ptr )
248
+ {
249
+ char str [] = "00\0" ;
250
+ int val ;
251
+ int ret ;
252
+
253
+ str [0 ] = fgetc (fw -> fp );
254
+ str [1 ] = fgetc (fw -> fp );
255
+
256
+ if (feof (fw -> fp )) return EOF ;
257
+
258
+ ret = sscanf (str , "%x" , & val );
259
+
260
+ * ptr = val ;
261
+
262
+ return ret ;
263
+ }
264
+
359
265
//******************************************************************************
360
266
/// \brief Send firmware frames to bootloader
361
267
/// \return #mxt_rc
@@ -396,14 +302,14 @@ static int send_frames(struct flash_context *fw)
396
302
397
303
frame = 1 ;
398
304
399
- while (!feof (fw -> fpc . fp )) {
305
+ while (!feof (fw -> fp )) {
400
306
if (frame_retry == 0 ) {
401
- if (fw -> fpc . get ( & fw -> fpc , & buffer [0 ]) == EOF ) {
307
+ if (get_hex_value ( fw , & buffer [0 ]) == EOF ) {
402
308
mxt_info (fw -> ctx , "End of file" );
403
309
break ;
404
310
}
405
311
406
- if (fw -> fpc . get ( & fw -> fpc , & buffer [1 ]) == EOF ) {
312
+ if (get_hex_value ( fw , & buffer [1 ]) == EOF ) {
407
313
mxt_err (fw -> ctx , "Unexpected end of firmware file" );
408
314
return MXT_ERROR_FILE_FORMAT ;
409
315
}
@@ -421,7 +327,7 @@ static int send_frames(struct flash_context *fw)
421
327
}
422
328
423
329
for (i = 2 ; i < frame_size ; i ++ ) {
424
- ret = fw -> fpc . get ( & fw -> fpc , & buffer [i ]);
330
+ ret = get_hex_value ( fw , & buffer [i ]);
425
331
426
332
if (ret == EOF ) {
427
333
mxt_err (fw -> ctx , "Unexpected end of firmware file" );
@@ -459,7 +365,7 @@ static int send_frames(struct flash_context *fw)
459
365
frame_retry = 0 ;
460
366
frame ++ ;
461
367
bytes_sent += frame_size ;
462
- cur_percent = (unsigned char )(0.5f + (100.0 * ftell (fw -> fpc . fp )) / fw -> file_size );
368
+ cur_percent = (unsigned char )(0.5f + (100.0 * ftell (fw -> fp )) / fw -> file_size );
463
369
464
370
/* Display at 10% or difference is greater than 10% */
465
371
if (cur_percent % 10 == 0 || (cur_percent - last_percent ) > 10 ) {
@@ -477,7 +383,7 @@ static int send_frames(struct flash_context *fw)
477
383
}
478
384
}
479
385
480
- fclose (fw -> fpc . fp );
386
+ fclose (fw -> fp );
481
387
482
388
return MXT_SUCCESS ;
483
389
}
@@ -640,59 +546,6 @@ static int mxt_enter_bootloader_mode(struct flash_context *fw)
640
546
return MXT_SUCCESS ;
641
547
}
642
548
643
- //******************************************************************************
644
- /// \brief Load firmware from .enc or binary file and save to new file in
645
- // .enc or binary format (automatically detect formats)
646
- /// \return #mxt_rc
647
- int mxt_convert_firmware_file (struct libmaxtouch_ctx * ctx ,
648
- const char * in_filename ,
649
- const char * out_filename )
650
- {
651
- int ret ;
652
- long file_size ;
653
- struct file_context in_fpc ;
654
- struct file_context out_fpc ;
655
- unsigned char val ;
656
-
657
- mxt_info (ctx , "Opening input firmware file %s" , in_filename );
658
-
659
- ret = open_file (& in_fpc , in_filename , "r" );
660
- if (ret ) {
661
- mxt_err (ctx , "Error %d opening %s for input" , ret , in_filename );
662
- ret = mxt_errno_to_rc (ret );
663
- goto fail_in ;
664
- }
665
-
666
- mxt_info (ctx , "Opening output firmware file %s" , out_filename );
667
-
668
- ret = open_file (& out_fpc , out_filename , "w" );
669
- if (ret ) {
670
- mxt_err (ctx , "Error %d opening %s for output" , ret , out_filename );
671
- ret = mxt_errno_to_rc (ret );
672
- goto fail_out ;
673
- }
674
-
675
- while (1 ) {
676
- if (in_fpc .get (& in_fpc , & val ) == EOF ) {
677
- break ;
678
- }
679
- ret = out_fpc .put (& out_fpc , val );
680
- if (ret ) {
681
- mxt_err (ctx , "Error %d writing to output" , ret );
682
- ret = mxt_errno_to_rc (ret );
683
- break ;
684
- }
685
- }
686
-
687
- fclose (out_fpc .fp );
688
-
689
- fail_out :
690
- fclose (in_fpc .fp );
691
-
692
- fail_in :
693
- return ret ;
694
- }
695
-
696
549
//******************************************************************************
697
550
/// \brief Flash firmware to chip
698
551
int mxt_flash_firmware (struct libmaxtouch_ctx * ctx ,
@@ -709,14 +562,14 @@ int mxt_flash_firmware(struct libmaxtouch_ctx *ctx,
709
562
710
563
mxt_info (fw .ctx , "Opening firmware file %s" , filename );
711
564
712
- ret = open_file ( & fw . fpc , filename , "r" );
713
- if (ret ) {
565
+ fw . fp = fopen ( filename , "r" );
566
+ if (! fw . fp ) {
714
567
mxt_err (fw .ctx , "Cannot open firmware file %s!" , filename );
715
- return mxt_errno_to_rc (ret );
568
+ return mxt_errno_to_rc (errno );
716
569
}
717
- fseek (fw .fpc . fp , 0L , SEEK_END );
718
- fw .file_size = ftell (fw .fpc . fp );
719
- rewind (fw .fpc . fp );
570
+ fseek (fw .fp , 0L , SEEK_END );
571
+ fw .file_size = ftell (fw .fp );
572
+ rewind (fw .fp );
720
573
721
574
ret = mxt_bootloader_init_chip (& fw );
722
575
if (ret && (ret != MXT_DEVICE_IN_BOOTLOADER ))
0 commit comments