Commit e026c367 authored by Piotr Dymacz's avatar Piotr Dymacz Committed by Fabio Utzig
Browse files

imgtool: sign: allow using 16-bit custom TLVs from valid range

The TLV type field in trailer TLV struct is defined as unsigned 16-bit
(in LE byte order). Currently, due to Python's struct format specified
as 'BBH', the 'imgtool' accepts only single byte for this field.

This results in error when trying to use 16-bit TLV type with option
'--custom-tlv':

  struct.error: ubyte format requires 0 <= number <= 255

This changes format to 'HH' which allows using 16-bit TLV types and
while at it, adds also simple range validation for custom TLV type.
As defined in image.h header, the vendor reserved TLVs value should
be from 0x00a0 to 0xfffe range.

Signed-off-by: default avatarPiotr Dymacz <pepe2k@gmail.com>
parent 4d51038c
Loading
Loading
Loading
Loading
+9 −1
Changes for scripts/imgtool/image.py: 9 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -81,6 +81,9 @@ TLV_INFO_SIZE = 4
TLV_INFO_MAGIC = 0x6907
TLV_PROT_INFO_MAGIC = 0x6908

TLV_VENDOR_RES_MIN = 0x00a0
TLV_VENDOR_RES_MAX = 0xfffe

STRUCT_ENDIAN_DICT = {
        'little': '<',
        'big':    '>'
@@ -111,7 +114,12 @@ class TLV():
        """
        e = STRUCT_ENDIAN_DICT[self.endian]
        if isinstance(kind, int):
            buf = struct.pack(e + 'BBH', kind, 0, len(payload))
            if not TLV_VENDOR_RES_MIN <= kind <= TLV_VENDOR_RES_MAX:
                msg = "Invalid custom TLV type value '0x{:04x}', allowed " \
                      "value should be between 0x{:04x} and 0x{:04x}".format(
                      kind, TLV_VENDOR_RES_MIN, TLV_VENDOR_RES_MAX)
                raise click.UsageError(msg)
            buf = struct.pack(e + 'HH', kind, len(payload))
        else:
            buf = struct.pack(e + 'BBH', TLV_VALUES[kind], 0, len(payload))
        self.buf += buf