Skip to content

Commit

Permalink
scripts: imgtool: Fix img verify for hex file format
Browse files Browse the repository at this point in the history
Currently imgtool --verify fails for hex files with:

Invalid image magic; is this an MCUboot image?

Added support for hex files by converting hex to bin
using IntelHex::tobinstr().

Reusing image.load() needs a bit of rework, maybe a
common load method will be done in the future,

Signed-off-by: Lucian Zala <[email protected]>
  • Loading branch information
lzala authored and valeo-artemis committed Jan 23, 2024
1 parent c3a72e9 commit b7a8e94
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions scripts/imgtool/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,15 @@ def pad_to(self, size):

@staticmethod
def verify(imgfile, key):
with open(imgfile, "rb") as f:
b = f.read()
ext = os.path.splitext(imgfile)[1][1:].lower()
try:
if ext == INTEL_HEX_EXT:
b = IntelHex(imgfile).tobinstr()
else:
with open(imgfile, 'rb') as f:
b = f.read()
except FileNotFoundError:
raise click.UsageError(f"Image file {imgfile} not found")

magic, _, header_size, _, img_size = struct.unpack('IIHHI', b[:16])
version = struct.unpack('BBHI', b[20:28])
Expand Down

0 comments on commit b7a8e94

Please sign in to comment.