Skip to content

Commit 5f53196

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: Ie563584c3f90e37002f9a01bb73ac1635b2b5e37
1 parent 16a5c66 commit 5f53196

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()
@@ -196,10 +197,11 @@ def __init__(self, version=None, header_size=IMAGE_HEADER_SIZE,
196197
lsb = self.max_align & 0x00ff
197198
msb = (self.max_align & 0xff00) >> 8
198199
align = bytes([msb, lsb]) if self.endian == "big" else bytes([lsb, msb])
199-
self.boot_magic = align + bytes([0x2d, 0xe1,
200-
0x5d, 0x29, 0x41, 0x0b,
201-
0x8d, 0x77, 0x67, 0x9c,
202-
0x11, 0x0f, 0x1f, 0x8a, ])
200+
self.boot_magic = align + bytes([
201+
0x2d, 0xe1,
202+
0x5d, 0x29, 0x41, 0x0b,
203+
0x8d, 0x77, 0x67, 0x9c,
204+
0x11, 0x0f, 0x1f, 0x8a, ])
203205

204206
if security_counter == 'auto':
205207
# Security counter has not been explicitly provided,
@@ -653,19 +655,23 @@ def verify(imgfile, key):
653655
except FileNotFoundError:
654656
raise click.UsageError(f"Image file not found: {imgfile}")
655657

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

659665
if magic != IMAGE_MAGIC:
660666
return VerifyResult.INVALID_MAGIC, None, None
661667

662668
tlv_off = header_size + img_size
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
if magic == TLV_PROT_INFO_MAGIC:
666672
tlv_off += tlv_tot
667673
tlv_info = b[tlv_off:tlv_off + TLV_INFO_SIZE]
668-
magic, tlv_tot = struct.unpack('HH', tlv_info)
674+
magic, tlv_tot = struct.unpack(e + 'HH', tlv_info)
669675

670676
if magic != TLV_INFO_MAGIC:
671677
return VerifyResult.INVALID_TLV_INFO_MAGIC, None, None
@@ -677,7 +683,7 @@ def verify(imgfile, key):
677683
tlv_off += TLV_INFO_SIZE # skip tlv info
678684
while tlv_off < tlv_end:
679685
tlv = b[tlv_off:tlv_off + TLV_SIZE]
680-
tlv_type, _, tlv_len = struct.unpack('BBH', tlv)
686+
tlv_type, _, tlv_len = struct.unpack(e + 'BBH', tlv)
681687
if tlv_type == TLV_VALUES["SHA256"] or tlv_type == TLV_VALUES["SHA384"]:
682688
if not tlv_matches_key_type(tlv_type, key):
683689
return VerifyResult.KEY_MISMATCH, None, None

0 commit comments

Comments
 (0)