Commit f3a57028 authored by Rustam Ismayilov's avatar Rustam Ismayilov Committed by Dávid Vincze
Browse files

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: default avatarRustam Ismayilov <rustam.ismayilov@arm.com>
Change-Id: I5b6e1dfa74606e2645a258065dd045cc8c7052c5
parent d16a613f
Loading
Loading
Loading
Loading
+17 −7
Changes for scripts/imgtool/dumpinfo.py: 17 added lines, 7 removed lines.
Original line number Diff line number Diff line
# Copyright 2023 Arm Limited
# Copyright 2023-2024 Arm Limited
#
# SPDX-License-Identifier: Apache-2.0
#
@@ -47,8 +47,12 @@ def print_tlv_records(tlv_list):
        print(" " * indent, "-" * 45)
        tlv_type, tlv_length, tlv_data = tlv.keys()

        if tlv[tlv_type] in TLV_TYPES:
            print(" " * indent, "{}: {} ({})".format(
                tlv_type, TLV_TYPES[tlv[tlv_type]], hex(tlv[tlv_type])))
        else:
            print(" " * indent, "{}: {} ({})".format(
                tlv_type, "UNKNOWN", hex(tlv[tlv_type])))
        print(" " * indent, "{}: ".format(tlv_length), hex(tlv[tlv_length]))
        print(" " * indent, "{}: ".format(tlv_data), end="")

@@ -60,7 +64,15 @@ def print_tlv_records(tlv_list):


def dump_imginfo(imgfile, outfile=None, silent=False):
    '''Parse a signed image binary and print/save the available information.'''
    """Parse a signed image binary and print/save the available information."""
    trailer_magic = None
    swap_size = 0
    swap_info = 0
    copy_done = 0
    image_ok = 0
    trailer = {}
    key_field_len = None

    try:
        with open(imgfile, "rb") as f:
            b = f.read()
@@ -128,7 +140,6 @@ def dump_imginfo(imgfile, outfile=None, silent=False):

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

            # Encryption key 0/1
            key_field_len = None
            if ((header["flags"] & image.IMAGE_F["ENCRYPTED_AES128"]) or
               (header["flags"] & image.IMAGE_F["ENCRYPTED_AES256"])):
                # The image is encrypted
@@ -200,7 +210,7 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
            else:
                flag_string = ""
                for flag in image.IMAGE_F.keys():
                    if (value & image.IMAGE_F[flag]):
                    if value & image.IMAGE_F[flag]:
                        if flag_string:
                            flag_string += ("\n" + (" " * 20))
                        flag_string += "{} ({})".format(
@@ -281,7 +291,7 @@ def dump_imginfo(imgfile, outfile=None, silent=False):
        print("boot magic:  ", end="")
        for i in range(BOOT_MAGIC_SIZE):
            print("{0:#04x}".format(trailer_magic[i]),  end=" ")
            if (i == (BOOT_MAGIC_SIZE/2 - 1)):
            if i == (BOOT_MAGIC_SIZE / 2 - 1):
                print("\n", end="             ")
        print()