@@ -28,15 +28,16 @@ typedef struct __attribute__((packed)) {
2828} UPFILE_ENTRY ;
2929
3030#define UPFILE_MAGIC 0x4b4d4e53 // "SNMK"
31+ #define UPFILE_FILE_COUNT 4
3132
32- static const char * FILE_TYPE_STRINGS [] = {
33+ static const char * FILE_TYPE_STRINGS [UPFILE_FILE_COUNT ] = {
3334 "SOC_FW" ,
3435 "MCU1_FW" ,
3536 "MCU2_FW" ,
3637 "MCU_DESC"
3738};
3839
39- static const char * FILE_NAMES [] = {
40+ static const char * FILE_NAMES [UPFILE_FILE_COUNT ] = {
4041 "update.img" ,
4142 "at32f403a.bin" ,
4243 "at32f415.bin" ,
@@ -86,7 +87,13 @@ static int info(const char *infile, const char *outdir, void (*file_fnc)(const c
8687 file_fnc ("UPFILE_BUILD_DATE" , header .build_date , trim_right (header .build_date , sizeof (header .build_date )));
8788 }
8889
89- for (uint8_t i = 0 ; i < be_to_host16 (header .files ); i ++ ) {
90+ uint16_t files = be_to_host16 (header .files );
91+ if (files > UPFILE_FILE_COUNT ) {
92+ fprintf (stderr , "Too many files found: %u\n" , files );
93+ goto error ;
94+ }
95+
96+ for (uint16_t i = 0 ; i < files ; i ++ ) {
9097 UPFILE_ENTRY entry ;
9198 if (fseek (fp , sizeof (UPFILE_HEADER ) + i * sizeof (UPFILE_ENTRY ), SEEK_SET ) != 0 ) {
9299 perror ("fseek" );
@@ -135,8 +142,7 @@ static int info(const char *infile, const char *outdir, void (*file_fnc)(const c
135142 }
136143
137144 if (file_fnc ) {
138- const char * filename = (i < ARRAY_SIZE (FILE_NAMES )) ? FILE_NAMES [i ] : "unknown" ;
139- file_fnc (filename , data , be_to_host32 (entry .size ));
145+ file_fnc (FILE_NAMES [i ], data , be_to_host32 (entry .size ));
140146 }
141147 free (data );
142148 }
@@ -260,7 +266,7 @@ static int pack(const char *outfile, const char *indir) {
260266 UPFILE_HEADER header = {0 };
261267 header .magic = UPFILE_MAGIC ;
262268 header .magic_ver = host_to_be16 (1 );
263- header .files = host_to_be16 (ARRAY_SIZE ( FILE_NAMES ) );
269+ header .files = host_to_be16 (UPFILE_FILE_COUNT );
264270 if (read_string_from_file ("UPFILE_VERSION" , header .version , sizeof (header .version )) != 0 ) {
265271 fprintf (stderr , "Error: UPFILE_VERSION not found\n" );
266272 goto error ;
@@ -278,11 +284,11 @@ static int pack(const char *outfile, const char *indir) {
278284 goto error ;
279285 }
280286
281- printf ("Packing UPFILE with %zu files\n" , ARRAY_SIZE ( FILE_NAMES ) );
287+ printf ("Packing UPFILE with %zu files\n" , UPFILE_FILE_COUNT );
282288
283- uint64_t data_offset = sizeof (UPFILE_HEADER ) + ARRAY_SIZE ( FILE_NAMES ) * sizeof (UPFILE_ENTRY );
289+ uint64_t data_offset = sizeof (UPFILE_HEADER ) + UPFILE_FILE_COUNT * sizeof (UPFILE_ENTRY );
284290
285- for (int i = 0 ; i < ARRAY_SIZE ( FILE_NAMES ) ; i ++ ) {
291+ for (int i = 0 ; i < UPFILE_FILE_COUNT ; i ++ ) {
286292 int ret = pack_file (outfp , i , i , FILE_NAMES [i ], & data_offset );
287293 if (ret != 0 ) {
288294 goto error ;
0 commit comments