Skip to content

Commit 271134c

Browse files
committed
fix(flash): Check for I/O failure during firmware file reading
1 parent 9b98858 commit 271134c

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/bl_install.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,15 @@ static int bootloader_send(FILE *fp, int length, u32* pCrc32)
261261
int bootloader_crc(FILE *fp, u32 starting_sector, u32 num_sectors, bool verbose)
262262
{
263263
u8 *firmware = calloc(FLASH_SIZE, 1);
264-
int read_bytes = fread(firmware, 1, FLASH_SIZE, fp);
265-
if (read_bytes != FLASH_SIZE) {
266-
printf("WARNING: firmware file might be truncated: %d bytes expected, %d bytes read\n", FLASH_SIZE, read_bytes);
264+
u32 read_bytes = fread(firmware, 1, FLASH_SIZE, fp);
265+
if (ferror(fp)) {
266+
errmsg = "Reading of firmware file failed";
267+
free(firmware);
268+
return ERR_IO;
269+
}
270+
u32 wanted_bytes = (starting_sector + num_sectors) * FLASH_SECTOR_SIZE;
271+
if (read_bytes < wanted_bytes) {
272+
printf("WARNING: firmware file is truncated: %d bytes expected, %d bytes read\n", wanted_bytes, read_bytes);
267273
}
268274

269275
int err = ERR_UNK;

0 commit comments

Comments
 (0)