Skip to content

Commit 895e103

Browse files
committed
imgtool: Enable big-endian support for verify command
Fix verify command fails for big endian images. Signed-off-by: Rustam Ismayilov <[email protected]> Change-Id: I94b888d64c2f33ccb829912491e3b8c44b9cc2b2
1 parent 80fd4df commit 895e103

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

scripts/imgtool/image.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from .keys import rsa, ecdsa, x25519
4242

4343
IMAGE_MAGIC = 0x96f3b83d
44+
IMAGE_MAGIC_LE = 0x3db8f396
4445
IMAGE_HEADER_SIZE = 32
4546
BIN_EXT = "bin"
4647
INTEL_HEX_EXT = "hex"
@@ -101,7 +102,7 @@ def align_up(num, align):
101102
return (num + (align - 1)) & ~(align - 1)
102103

103104

104-
class TLV():
105+
class TLV:
105106
def __init__(self, endian, magic=TLV_INFO_MAGIC):
106107
self.magic = magic
107108
self.buf = bytearray()
@@ -194,10 +195,11 @@ def __init__(self, version=None, header_size=IMAGE_HEADER_SIZE,
194195
lsb = self.max_align & 0x00ff
195196
msb = (self.max_align & 0xff00) >> 8
196197
align = bytes([msb, lsb]) if self.endian == "big" else bytes([lsb, msb])
197-
self.boot_magic = align + bytes([0x2d, 0xe1,
198-
0x5d, 0x29, 0x41, 0x0b,
199-
0x8d, 0x77, 0x67, 0x9c,
200-
0x11, 0x0f, 0x1f, 0x8a, ])
198+
self.boot_magic = align + bytes([
199+
0x2d, 0xe1,
200+
0x5d, 0x29, 0x41, 0x0b,
201+
0x8d, 0x77, 0x67, 0x9c,
202+
0x11, 0x0f, 0x1f, 0x8a, ])
201203

202204
if security_counter == 'auto':
203205
# Security counter has not been explicitly provided,
@@ -649,19 +651,23 @@ def verify(imgfile, key):
649651
except FileNotFoundError:
650652
raise click.UsageError(f"Image file not found: {imgfile}")
651653

652-
magic, _, header_size, _, img_size = struct.unpack('IIHHI', b[:16])
653-
version = struct.unpack('BBHI', b[20:28])
654+
# Detect image byteorder by image magic
655+
magic = int.from_bytes(b[:4], "big")
656+
e = '<' if magic == IMAGE_MAGIC_LE else '>'
657+
658+
magic, _, header_size, _, img_size = struct.unpack(e + 'IIHHI', b[:16])
659+
version = struct.unpack(e + 'BBHI', b[20:28])
654660

655661
if magic != IMAGE_MAGIC:
656662
return VerifyResult.INVALID_MAGIC, None, None
657663

658664
tlv_off = header_size + img_size
659665
tlv_info = b[tlv_off:tlv_off + TLV_INFO_SIZE]
660-
magic, tlv_tot = struct.unpack('HH', tlv_info)
666+
magic, tlv_tot = struct.unpack(e + 'HH', tlv_info)
661667
if magic == TLV_PROT_INFO_MAGIC:
662668
tlv_off += tlv_tot
663669
tlv_info = b[tlv_off:tlv_off + TLV_INFO_SIZE]
664-
magic, tlv_tot = struct.unpack('HH', tlv_info)
670+
magic, tlv_tot = struct.unpack(e + 'HH', tlv_info)
665671

666672
if magic != TLV_INFO_MAGIC:
667673
return VerifyResult.INVALID_TLV_INFO_MAGIC, None, None
@@ -673,7 +679,7 @@ def verify(imgfile, key):
673679
tlv_off += TLV_INFO_SIZE # skip tlv info
674680
while tlv_off < tlv_end:
675681
tlv = b[tlv_off:tlv_off + TLV_SIZE]
676-
tlv_type, _, tlv_len = struct.unpack('BBH', tlv)
682+
tlv_type, _, tlv_len = struct.unpack(e + 'BBH', tlv)
677683
if tlv_type == TLV_VALUES["SHA256"] or tlv_type == TLV_VALUES["SHA384"]:
678684
if not tlv_matches_key_type(tlv_type, key):
679685
return VerifyResult.KEY_MISMATCH, None, None

0 commit comments

Comments
 (0)