Commit a66c6873 authored by Mark Schulte's avatar Mark Schulte Committed by Fabio Utzig
Browse files

[imgtool] Add big endian support

Add big endian support to imgtool so that it can be used on big endian
targets.

Signed-off-by: default avatarMark Schulte <mschulte@lyft.com>
parent 08fa2672
Loading
Loading
Loading
Loading
+5 −2
Changes for scripts/imgtool.py: 5 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -114,6 +114,8 @@ class BasedIntParamType(click.ParamType):

@click.argument('outfile')
@click.argument('infile')
@click.option('-e', '--endian', type=click.Choice(['little', 'big']),
              default='little', help="Select little or big endian")
@click.option('--overwrite-only', default=False, is_flag=True,
              help='Use overwrite-only instead of swap upgrades')
@click.option('-M', '--max-sectors', type=int,
@@ -131,12 +133,13 @@ class BasedIntParamType(click.ParamType):
@click.option('-k', '--key', metavar='filename')
@click.command(help='Create a signed or unsigned image')
def sign(key, align, version, header_size, pad_header, slot_size, pad,
         max_sectors, overwrite_only, infile, outfile):
         max_sectors, overwrite_only, endian, infile, outfile):
    img = image.Image.load(infile, version=decode_version(version),
                           header_size=header_size, pad_header=pad_header,
                           pad=pad, align=int(align), slot_size=slot_size,
                           max_sectors=max_sectors,
                           overwrite_only=overwrite_only)
                           overwrite_only=overwrite_only,
                           endian=endian)
    key = load_key(key) if key else None
    img.sign(key)

+18 −7
Changes for scripts/imgtool/image.py: 18 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -50,18 +50,26 @@ boot_magic = bytes([
    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 @@ class Image():

    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 @@ class Image():
        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 @@ class Image():
                    self.slot_size,
                    self.max_sectors,
                    self.overwrite_only,
                    self.endian,
                    self.__class__.__name__,
                    len(self.payload))

@@ -131,7 +141,7 @@ class Image():
    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 @@ class Image():

        flags = 0

        fmt = ('<' +
        e = STRUCT_ENDIAN_DICT[self.endian]
        fmt = (e +
            # type ImageHdr struct {
            'I' +   # Magic uint32
            'I' +   # LoadAddr uint32