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: Piotr Dymacz <pepe2k@gmail.com>
diff --git a/scripts/imgtool/image.py b/scripts/imgtool/image.py
index 3148a67..5114bf4 100644
--- a/scripts/imgtool/image.py
+++ b/scripts/imgtool/image.py
@@ -81,6 +81,9 @@
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 @@
"""
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