Skip to content

Commit

Permalink
image: fix binary detection for small files
Browse files Browse the repository at this point in the history
Previously, if the image file was less than 9 bytes long,
it was assumed to be an error when it could be a binary
image file. This patch makes OpenOCD detect these cases
as binary files.

Change-Id: I5b4dad2b547786246887812ac75907378fe58671
Signed-off-by: Marek Vrbka <[email protected]>
Reviewed-on: https://review.openocd.org/c/openocd/+/7880
Tested-by: jenkins
Reviewed-by: Tomas Vanek <[email protected]>
Reviewed-by: Antonio Borneo <[email protected]>
  • Loading branch information
MarekVCodasip authored and borneoa committed Sep 8, 2023
1 parent 62f76b2 commit d41a204
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/target/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ static int autodetect_image_type(struct image *image, const char *url)
if (retval != ERROR_OK)
return retval;
retval = fileio_read(fileio, 9, buffer, &read_bytes);
fileio_close(fileio);

if (retval == ERROR_OK) {
if (read_bytes != 9)
retval = ERROR_FILEIO_OPERATION_FAILED;
/* If the file is smaller than 9 bytes, it can only be bin */
if (retval == ERROR_OK && read_bytes != 9) {
LOG_DEBUG("Less than 9 bytes in the image file found.");
LOG_DEBUG("BIN image detected.");
image->type = IMAGE_BINARY;
return ERROR_OK;
}
fileio_close(fileio);

if (retval != ERROR_OK)
return retval;
Expand All @@ -82,8 +85,10 @@ static int autodetect_image_type(struct image *image, const char *url)
&& (buffer[1] >= '0') && (buffer[1] < '9')) {
LOG_DEBUG("S19 image detected.");
image->type = IMAGE_SRECORD;
} else
} else {
LOG_DEBUG("BIN image detected.");
image->type = IMAGE_BINARY;
}

return ERROR_OK;
}
Expand Down

0 comments on commit d41a204

Please sign in to comment.