26
26
from intelhex import IntelHex
27
27
28
28
from imgtool import image
29
- from imgtool .image import INTEL_HEX_EXT
29
+ from imgtool .image import INTEL_HEX_EXT , IMAGE_MAGIC_LE
30
30
31
31
HEADER_ITEMS = ("magic" , "load_addr" , "hdr_size" , "protected_tlv_size" ,
32
32
"img_size" , "flags" , "version" )
@@ -142,8 +142,12 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
142
142
except FileNotFoundError :
143
143
raise click .UsageError (f"Image file not found: { imgfile } " )
144
144
145
+ # Detect image byteorder by image magic
146
+ magic = int .from_bytes (b [:4 ], "big" )
147
+ order = '<' if magic == IMAGE_MAGIC_LE else '>'
148
+
145
149
# Parsing the image header
146
- _header = struct .unpack ('IIHHIIBBHI' , b [:28 ])
150
+ _header = struct .unpack (order + 'IIHHIIBBHI' , b [:28 ])
147
151
# Image version consists of the last 4 item ('BBHI')
148
152
_version = _header [- 4 :]
149
153
header = {}
@@ -162,9 +166,8 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
162
166
protected_tlv_size = header ["protected_tlv_size" ]
163
167
164
168
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 )])
168
171
tlv_area ["tlv_hdr_prot" ]["magic" ] = _tlv_prot_head [0 ]
169
172
tlv_area ["tlv_hdr_prot" ]["tlv_tot" ] = _tlv_prot_head [1 ]
170
173
tlv_end = tlv_off + tlv_area ["tlv_hdr_prot" ]["tlv_tot" ]
@@ -173,15 +176,15 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
173
176
# Iterating through the protected TLV area
174
177
while tlv_off < tlv_end :
175
178
tlv_type , tlv_len = struct .unpack (
176
- 'HH' ,
179
+ order + 'HH' ,
177
180
b [tlv_off :(tlv_off + image .TLV_INFO_SIZE )])
178
181
tlv_off += image .TLV_INFO_SIZE
179
182
tlv_data = b [tlv_off :(tlv_off + tlv_len )]
180
183
tlv_area ["tlvs_prot" ].append (
181
184
{"type" : tlv_type , "len" : tlv_len , "data" : tlv_data })
182
185
tlv_off += tlv_len
183
186
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 )])
185
188
tlv_area ["tlv_hdr" ]["magic" ] = _tlv_head [0 ]
186
189
tlv_area ["tlv_hdr" ]["tlv_tot" ] = _tlv_head [1 ]
187
190
@@ -190,9 +193,8 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
190
193
191
194
# Iterating through the TLV area
192
195
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 )])
196
198
tlv_off += image .TLV_INFO_SIZE
197
199
tlv_data = b [tlv_off :(tlv_off + tlv_len )]
198
200
tlv_area ["tlvs" ].append (
@@ -212,7 +214,7 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
212
214
max_align = 8
213
215
elif trailer_magic [- len (BOOT_MAGIC_2 ):] == BOOT_MAGIC_2 :
214
216
# 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" )
216
218
else :
217
219
# Invalid magic: the rest of the image trailer cannot be processed.
218
220
print ("Warning: the trailer magic value is invalid!" )
@@ -235,7 +237,7 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
235
237
236
238
trailer_off -= max_align
237
239
swap_size = int .from_bytes (b [trailer_off :(trailer_off + 4 )],
238
- "little" )
240
+ "big" if order == '>' else " little" )
239
241
trailer ["swap_size" ] = swap_size
240
242
241
243
# Encryption key 0/1
0 commit comments