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: Fabio Utzig <utzig@apache.org>
diff --git a/scripts/imgtool/image.py b/scripts/imgtool/image.py
index d159e7a..78600dd 100644
--- a/scripts/imgtool/image.py
+++ b/scripts/imgtool/image.py
@@ -164,20 +164,26 @@
 
         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)
 
@@ -341,7 +347,7 @@
         tsize = self._trailer_size(self.align, self.max_sectors,
                                    self.overwrite_only, self.enckey)
         padding = size - (len(self.payload) + tsize)
-        pbytes  = b'\xff' * padding
+        pbytes = b'\xff' * padding
         pbytes += b'\xff' * (tsize - len(boot_magic))
         pbytes += boot_magic
         self.payload += pbytes