[imgtool] Add big endian support
Add big endian support to imgtool so that it can be used on big endian
targets.
Signed-off-by: Mark Schulte <mschulte@lyft.com>
diff --git a/scripts/imgtool/image.py b/scripts/imgtool/image.py
index 1e8d243..7a6111d 100644
--- a/scripts/imgtool/image.py
+++ b/scripts/imgtool/image.py
@@ -50,18 +50,26 @@
0x35, 0x52, 0x50, 0x0f,
0x2c, 0xb6, 0x79, 0x80, ])
+STRUCT_ENDIAN_DICT = {
+ 'little': '<',
+ 'big': '>'
+}
+
class TLV():
- def __init__(self):
+ def __init__(self, endian):
self.buf = bytearray()
+ self.endian = endian
def add(self, kind, payload):
"""Add a TLV record. Kind should be a string found in TLV_VALUES above."""
- buf = struct.pack('<BBH', TLV_VALUES[kind], 0, len(payload))
+ e = STRUCT_ENDIAN_DICT[self.endian]
+ buf = struct.pack(e + 'BBH', TLV_VALUES[kind], 0, len(payload))
self.buf += buf
self.buf += payload
def get(self):
- header = struct.pack('<HH', TLV_INFO_MAGIC, TLV_INFO_SIZE + len(self.buf))
+ e = STRUCT_ENDIAN_DICT[self.endian]
+ header = struct.pack(e + 'HH', TLV_INFO_MAGIC, TLV_INFO_SIZE + len(self.buf))
return header + bytes(self.buf)
class Image():
@@ -89,7 +97,7 @@
def __init__(self, version=None, header_size=IMAGE_HEADER_SIZE, pad=0,
align=1, slot_size=0, max_sectors=DEFAULT_MAX_SECTORS,
- overwrite_only=False):
+ overwrite_only=False, endian="little"):
self.version = version or versmod.decode_version("0")
self.header_size = header_size or IMAGE_HEADER_SIZE
self.pad = pad
@@ -97,11 +105,12 @@
self.slot_size = slot_size
self.max_sectors = max_sectors
self.overwrite_only = overwrite_only
+ self.endian = endian
def __repr__(self):
return "<Image version={}, header_size={}, base_addr={}, \
align={}, slot_size={}, max_sectors={}, overwrite_only={}, \
- format={}, payloadlen=0x{:x}>".format(
+ endian={} format={}, payloadlen=0x{:x}>".format(
self.version,
self.header_size,
self.base_addr if self.base_addr is not None else "N/A",
@@ -109,6 +118,7 @@
self.slot_size,
self.max_sectors,
self.overwrite_only,
+ self.endian,
self.__class__.__name__,
len(self.payload))
@@ -131,7 +141,7 @@
def sign(self, key):
self.add_header(key)
- tlv = TLV()
+ tlv = TLV(self.endian)
# Note that ecdsa wants to do the hashing itself, which means
# we get to hash it twice.
@@ -161,7 +171,8 @@
flags = 0
- fmt = ('<' +
+ e = STRUCT_ENDIAN_DICT[self.endian]
+ fmt = (e +
# type ImageHdr struct {
'I' + # Magic uint32
'I' + # LoadAddr uint32