Skip to content

Commit f3a5702

Browse files
rustammendeldavidvincze
authored andcommitted
imgtool: Various dumpinfo fixes
Add support for dumping images with custom tlv Fix uninitialized access to some variables Fix trailer magic detection Fix some linting issues (parenthesis, indentation, spacing) Signed-off-by: Rustam Ismayilov <[email protected]> Change-Id: I5b6e1dfa74606e2645a258065dd045cc8c7052c5
1 parent d16a613 commit f3a5702

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

scripts/imgtool/dumpinfo.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2023 Arm Limited
1+
# Copyright 2023-2024 Arm Limited
22
#
33
# SPDX-License-Identifier: Apache-2.0
44
#
@@ -28,15 +28,15 @@
2828
"img_size", "flags", "version")
2929
TLV_TYPES = dict((value, key) for key, value in image.TLV_VALUES.items())
3030
BOOT_MAGIC = bytes([
31-
0x77, 0xc2, 0x95, 0xf3,
32-
0x60, 0xd2, 0xef, 0x7f,
33-
0x35, 0x52, 0x50, 0x0f,
34-
0x2c, 0xb6, 0x79, 0x80, ])
31+
0x77, 0xc2, 0x95, 0xf3,
32+
0x60, 0xd2, 0xef, 0x7f,
33+
0x35, 0x52, 0x50, 0x0f,
34+
0x2c, 0xb6, 0x79, 0x80, ])
3535
BOOT_MAGIC_2 = bytes([
36-
0x2d, 0xe1, 0x5d, 0x29,
37-
0x41, 0x0b, 0x8d, 0x77,
38-
0x67, 0x9c, 0x11, 0x0f,
39-
0x1f, 0x8a, ])
36+
0x2d, 0xe1, 0x5d, 0x29,
37+
0x41, 0x0b, 0x8d, 0x77,
38+
0x67, 0x9c, 0x11, 0x0f,
39+
0x1f, 0x8a, ])
4040
BOOT_MAGIC_SIZE = len(BOOT_MAGIC)
4141
_LINE_LENGTH = 60
4242

@@ -47,20 +47,32 @@ def print_tlv_records(tlv_list):
4747
print(" " * indent, "-" * 45)
4848
tlv_type, tlv_length, tlv_data = tlv.keys()
4949

50-
print(" " * indent, "{}: {} ({})".format(
50+
if tlv[tlv_type] in TLV_TYPES:
51+
print(" " * indent, "{}: {} ({})".format(
5152
tlv_type, TLV_TYPES[tlv[tlv_type]], hex(tlv[tlv_type])))
53+
else:
54+
print(" " * indent, "{}: {} ({})".format(
55+
tlv_type, "UNKNOWN", hex(tlv[tlv_type])))
5256
print(" " * indent, "{}: ".format(tlv_length), hex(tlv[tlv_length]))
5357
print(" " * indent, "{}: ".format(tlv_data), end="")
5458

5559
for j, data in enumerate(tlv[tlv_data]):
56-
print("{0:#04x}".format(data), end=" ")
57-
if ((j+1) % 8 == 0) and ((j+1) != len(tlv[tlv_data])):
58-
print("\n", end=" " * (indent+7))
60+
print("{0:#04x}".format(data), end=" ")
61+
if ((j + 1) % 8 == 0) and ((j + 1) != len(tlv[tlv_data])):
62+
print("\n", end=" " * (indent + 7))
5963
print()
6064

6165

6266
def dump_imginfo(imgfile, outfile=None, silent=False):
63-
'''Parse a signed image binary and print/save the available information.'''
67+
"""Parse a signed image binary and print/save the available information."""
68+
trailer_magic = None
69+
swap_size = 0
70+
swap_info = 0
71+
copy_done = 0
72+
image_ok = 0
73+
trailer = {}
74+
key_field_len = None
75+
6476
try:
6577
with open(imgfile, "rb") as f:
6678
b = f.read()
@@ -128,7 +140,6 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
128140

129141
if _img_pad_size:
130142
# Parsing the image trailer
131-
trailer = {}
132143
trailer_off = -BOOT_MAGIC_SIZE
133144
trailer_magic = b[trailer_off:]
134145
trailer["magic"] = trailer_magic
@@ -138,7 +149,7 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
138149
max_align = 8
139150
elif trailer_magic[-len(BOOT_MAGIC_2):] == BOOT_MAGIC_2:
140151
# The alignment value is encoded in the magic field
141-
max_align = int(trailer_magic[:2], 0)
152+
max_align = int.from_bytes(trailer_magic[:2], "little")
142153
else:
143154
# Invalid magic: the rest of the image trailer cannot be processed.
144155
print("Warning: the trailer magic value is invalid!")
@@ -165,7 +176,6 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
165176
trailer["swap_size"] = swap_size
166177

167178
# Encryption key 0/1
168-
key_field_len = None
169179
if ((header["flags"] & image.IMAGE_F["ENCRYPTED_AES128"]) or
170180
(header["flags"] & image.IMAGE_F["ENCRYPTED_AES256"])):
171181
# The image is encrypted
@@ -200,7 +210,7 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
200210
else:
201211
flag_string = ""
202212
for flag in image.IMAGE_F.keys():
203-
if (value & image.IMAGE_F[flag]):
213+
if value & image.IMAGE_F[flag]:
204214
if flag_string:
205215
flag_string += ("\n" + (" " * 20))
206216
flag_string += "{} ({})".format(
@@ -209,7 +219,7 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
209219

210220
if type(value) != str:
211221
value = hex(value)
212-
print(key, ":", " " * (19-len(key)), value, sep="")
222+
print(key, ":", " " * (19 - len(key)), value, sep="")
213223
print("#" * _LINE_LENGTH)
214224

215225
# Image payload
@@ -281,7 +291,7 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
281291
print("boot magic: ", end="")
282292
for i in range(BOOT_MAGIC_SIZE):
283293
print("{0:#04x}".format(trailer_magic[i]), end=" ")
284-
if (i == (BOOT_MAGIC_SIZE/2 - 1)):
294+
if i == (BOOT_MAGIC_SIZE / 2 - 1):
285295
print("\n", end=" ")
286296
print()
287297

0 commit comments

Comments
 (0)