Commit edbabcf6 authored by Fabio Utzig's avatar Fabio Utzig Committed by Fabio Utzig
Browse files

imgtool: allow address adjustment in output hex

Add a new flag `-x` (or `--hex_addr`) which adjusts the memory address
where this file has to be written to. This is useful when generating
upgrade images that will go to the secondary slot, in cases where the
user is not using mcumgr or some other delivery mechanism, and has to
manually adjust the addresses using `objcopy`.

Also when using hex files, image padding now only adds a segment with
the magic at the end instead of filling the whole output with `0xff`.

Signed-off-by: default avatarFabio Utzig <utzig@apache.org>
parent 13189380
Loading
Loading
Loading
Loading
+12 −6
Changes for scripts/imgtool/image.py: 12 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -164,20 +164,26 @@ class Image():

        self.check()

    def save(self, path):
    def save(self, path, hex_addr=None):
        """Save an image from a given file"""
        if self.pad:
            self.pad_to(self.slot_size)

        ext = os.path.splitext(path)[1][1:].lower()
        if ext == INTEL_HEX_EXT:
            # input was in binary format, but HEX needs to know the base addr
            if self.base_addr is None:
                raise Exception("Input file does not provide a base address")
            if self.base_addr is None and hex_addr is None:
                raise Exception("No address exists in input file neither was "
                                "it provided by user")
            h = IntelHex()
            if hex_addr is not None:
                self.base_addr = hex_addr
            h.frombytes(bytes=self.payload, offset=self.base_addr)
            if self.pad:
                magic_addr = (self.base_addr + self.slot_size) - \
                    len(boot_magic)
                h.puts(magic_addr, boot_magic)
            h.tofile(path, 'hex')
        else:
            if self.pad:
                self.pad_to(self.slot_size)
            with open(path, 'wb') as f:
                f.write(self.payload)

+4 −2
Changes for scripts/imgtool/main.py: 4 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -182,6 +182,8 @@ class BasedIntParamType(click.ParamType):

@click.argument('outfile')
@click.argument('infile')
@click.option('-x', '--hex-addr', type=BasedIntParamType(), required=False,
              help='Adjust address in hex output file.')
@click.option('-L', '--load-addr', type=BasedIntParamType(), required=False,
              help='Load address for image when it is in its primary slot.')
@click.option('-E', '--encrypt', metavar='filename',
@@ -212,7 +214,7 @@ class BasedIntParamType(click.ParamType):
               .hex extension, otherwise binary format is used''')
def sign(key, align, version, header_size, pad_header, slot_size, pad,
         max_sectors, overwrite_only, endian, encrypt, infile, outfile,
         dependencies, load_addr):
         dependencies, load_addr, hex_addr):
    img = image.Image(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,
@@ -227,7 +229,7 @@ def sign(key, align, version, header_size, pad_header, slot_size, pad,
        if key and not isinstance(key, keys.RSA):
            raise Exception("Signing only available with private RSA key")
    img.create(key, enckey, dependencies)
    img.save(outfile)
    img.save(outfile, hex_addr)


class AliasesGroup(click.Group):