imgtool: Adjust base_addr when injecting header

If adding a header to a .hex file (using the command line
"--pad-header"), the hex file gets shifted by header size. This may
cause hard coded addresses to be off by "header-size", making the
firmware unusable. Instead, adjust the base address by the header
size, so the existing firmware in the hex file is loaded to the
proper addresses.

Signed-off-by: Mark Schulte <mschulte@lyft.com>
diff --git a/scripts/imgtool/image.py b/scripts/imgtool/image.py
index 511c78c..1e8d243 100644
--- a/scripts/imgtool/image.py
+++ b/scripts/imgtool/image.py
@@ -79,6 +79,9 @@
 
         # Add the image header if needed.
         if pad_header and obj.header_size > 0:
+            if obj.base_addr:
+                # Adjust base_addr for new header
+                obj.base_addr -= obj.header_size
             obj.payload = (b'\000' * obj.header_size) + obj.payload
 
         obj.check()