summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoakim Bech <joakim.bech@linaro.org>2016-08-30 09:49:00 +0200
committerJoakim Bech <joakim.bech@linaro.org>2016-08-30 09:49:00 +0200
commit777fc00c7c7055dabf4db119fc2b9f62c8d30469 (patch)
treefea47e6c03a4cf49bc638eff0ea1cffe6fdede49
parent2ddfd8a650d544b2c8d56d28a02a9d3ff355ae2f (diff)
Add a function to create a the header
Signed-off-by: Joakim Bech <joakim.bech@linaro.org>
-rw-r--r--newtimg.py3
-rwxr-xr-xpatch_mynewt_img.py22
2 files changed, 23 insertions, 2 deletions
diff --git a/newtimg.py b/newtimg.py
index 8124f0b..ca55307 100644
--- a/newtimg.py
+++ b/newtimg.py
@@ -1,4 +1,4 @@
-IMAGE_MAGIC = 0x96f3b83
+IMAGE_MAGIC = 0x96f3b83c
IMAGE_MAGIC_NONE = 0xffffffff
#
@@ -21,6 +21,7 @@ IMAGE_TLV_RSA2048 = 2 # RSA2048 of hash output
IMAGE_TLV_ECDSA224 = 3 # ECDSA of hash output
+OFFSET_VECTOR_TABLE = 0x80
OFFSET_IH_MAGIC = 0
OFFSET_IH_TLV_SIZE = 4
OFFSET_IH_KEY_ID = 6
diff --git a/patch_mynewt_img.py b/patch_mynewt_img.py
index 7f407b6..6f0a0bc 100755
--- a/patch_mynewt_img.py
+++ b/patch_mynewt_img.py
@@ -7,6 +7,24 @@ import sys
from newtimg import *
from ctypes import *
+def create_header(img):
+ hdr = bytearray(struct.pack('I', IMAGE_MAGIC) +
+ struct.pack('H', SHA256_DIGEST_SIZE + 4) + # TLV size
+ struct.pack('B', 0) + # Key ID
+ struct.pack('B', 0) + # PAD 1
+ struct.pack('H', OFFSET_VECTOR_TABLE) + # New HDR SIZE
+ struct.pack('H', 0) + # PAD 2
+ struct.pack('I', 0xbabebabe) + # TODO img size
+ struct.pack('I', IMAGE_F_SHA256) + # Flags
+ struct.pack('B', 0) + # Major
+ struct.pack('B', 0) + # Minor
+ struct.pack('H', 0) + # Revision
+ struct.pack('I', 0) + # Build number
+ struct.pack('I', 0)) # PAD3
+ with open(img + ".hdr", "w+b") as f:
+ f.write(hdr)
+ f.close()
+
def patch_header(img):
"""
Patch the image with the new header size and with the new image size.
@@ -49,7 +67,8 @@ def extend_image(img, img_size):
# Store the header, pad it and then store the image itself
with open(img + ".img", "w+b") as f:
f.write(hdr)
- f.write(('\xFF' * (0x80 - IMAGE_HEADER_SIZE)))
+ # Pad the header up til where the image itself begins
+ f.write(('\xFF' * (OFFSET_VECTOR_TABLE - IMAGE_HEADER_SIZE)))
f.write(image)
f.close()
except IOError:
@@ -84,6 +103,7 @@ def append_hash(img, digest):
def main(img):
# backup(img) # Backup original image
+ create_header(img)
size = patch_header(img)
extend_image(img, size)