Skip to content

Commit 5e3cae1

Browse files
committed
imgtool: Enable big-endian support for dumpinfo command
Add support for big-endian images. Signed-off-by: Rustam Ismayilov <[email protected]> Change-Id: Ib664571bce71ceb4878525d0d4797963b3c64dbf
1 parent 895e103 commit 5e3cae1

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

scripts/imgtool/dumpinfo.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from intelhex import IntelHex
2727

2828
from imgtool import image
29-
from imgtool.image import INTEL_HEX_EXT
29+
from imgtool.image import INTEL_HEX_EXT, IMAGE_MAGIC_LE
3030

3131
HEADER_ITEMS = ("magic", "load_addr", "hdr_size", "protected_tlv_size",
3232
"img_size", "flags", "version")
@@ -142,8 +142,12 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
142142
except FileNotFoundError:
143143
raise click.UsageError(f"Image file not found: {imgfile}")
144144

145+
# Detect image byteorder by image magic
146+
magic = int.from_bytes(b[:4], "big")
147+
order = '<' if magic == IMAGE_MAGIC_LE else '>'
148+
145149
# Parsing the image header
146-
_header = struct.unpack('IIHHIIBBHI', b[:28])
150+
_header = struct.unpack(order + 'IIHHIIBBHI', b[:28])
147151
# Image version consists of the last 4 item ('BBHI')
148152
_version = _header[-4:]
149153
header = {}
@@ -162,9 +166,8 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
162166
protected_tlv_size = header["protected_tlv_size"]
163167

164168
if protected_tlv_size != 0:
165-
_tlv_prot_head = struct.unpack(
166-
'HH',
167-
b[tlv_off:(tlv_off + image.TLV_INFO_SIZE)])
169+
_tlv_prot_head = struct.unpack(order + 'HH',
170+
b[tlv_off:(tlv_off + image.TLV_INFO_SIZE)])
168171
tlv_area["tlv_hdr_prot"]["magic"] = _tlv_prot_head[0]
169172
tlv_area["tlv_hdr_prot"]["tlv_tot"] = _tlv_prot_head[1]
170173
tlv_end = tlv_off + tlv_area["tlv_hdr_prot"]["tlv_tot"]
@@ -173,15 +176,15 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
173176
# Iterating through the protected TLV area
174177
while tlv_off < tlv_end:
175178
tlv_type, tlv_len = struct.unpack(
176-
'HH',
179+
order + 'HH',
177180
b[tlv_off:(tlv_off + image.TLV_INFO_SIZE)])
178181
tlv_off += image.TLV_INFO_SIZE
179182
tlv_data = b[tlv_off:(tlv_off + tlv_len)]
180183
tlv_area["tlvs_prot"].append(
181184
{"type": tlv_type, "len": tlv_len, "data": tlv_data})
182185
tlv_off += tlv_len
183186

184-
_tlv_head = struct.unpack('HH', b[tlv_off:(tlv_off + image.TLV_INFO_SIZE)])
187+
_tlv_head = struct.unpack(order + 'HH', b[tlv_off:(tlv_off + image.TLV_INFO_SIZE)])
185188
tlv_area["tlv_hdr"]["magic"] = _tlv_head[0]
186189
tlv_area["tlv_hdr"]["tlv_tot"] = _tlv_head[1]
187190

@@ -190,9 +193,8 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
190193

191194
# Iterating through the TLV area
192195
while tlv_off < tlv_end:
193-
tlv_type, tlv_len = struct.unpack(
194-
'HH',
195-
b[tlv_off:(tlv_off + image.TLV_INFO_SIZE)])
196+
tlv_type, _, tlv_len = struct.unpack(order + 'BBH',
197+
b[tlv_off:(tlv_off + image.TLV_INFO_SIZE)])
196198
tlv_off += image.TLV_INFO_SIZE
197199
tlv_data = b[tlv_off:(tlv_off + tlv_len)]
198200
tlv_area["tlvs"].append(
@@ -212,7 +214,7 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
212214
max_align = 8
213215
elif trailer_magic[-len(BOOT_MAGIC_2):] == BOOT_MAGIC_2:
214216
# The alignment value is encoded in the magic field
215-
max_align = int.from_bytes(trailer_magic[:2], "little")
217+
max_align = int.from_bytes(trailer_magic[:2], "big" if order == '>' else "little")
216218
else:
217219
# Invalid magic: the rest of the image trailer cannot be processed.
218220
print("Warning: the trailer magic value is invalid!")
@@ -235,7 +237,7 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
235237

236238
trailer_off -= max_align
237239
swap_size = int.from_bytes(b[trailer_off:(trailer_off + 4)],
238-
"little")
240+
"big" if order == '>' else "little")
239241
trailer["swap_size"] = swap_size
240242

241243
# Encryption key 0/1

0 commit comments

Comments
 (0)