diff options
author | Joakim Bech <joakim.bech@linaro.org> | 2016-08-30 14:20:47 +0200 |
---|---|---|
committer | Joakim Bech <joakim.bech@linaro.org> | 2016-08-30 14:20:47 +0200 |
commit | 65f1433de00de2bd58a59eec02be08dfea122a30 (patch) | |
tree | 4402e4e99269d71d6104cfe312f2c4c6fa4cd3eb | |
parent | f5172cbf444baeee08e9e622fc208cf88a21a1d2 (diff) |
Create image from Zephyr bin-file
Signed-off-by: Joakim Bech <joakim.bech@linaro.org>
-rwxr-xr-x | patch_mynewt_img.py | 64 |
1 files changed, 14 insertions, 50 deletions
diff --git a/patch_mynewt_img.py b/patch_mynewt_img.py index 8720e7c..a7bd521 100755 --- a/patch_mynewt_img.py +++ b/patch_mynewt_img.py @@ -22,7 +22,7 @@ def create_header(img): struct.pack('H', 0) + # PAD 2 struct.pack('I', image_size) + # TODO img size struct.pack('I', IMAGE_F_SHA256) + # Flags - struct.pack('B', 0) + # Major + struct.pack('B', 1) + # Major struct.pack('B', 0) + # Minor struct.pack('H', 0) + # Revision struct.pack('I', 0) + # Build number @@ -34,60 +34,26 @@ def create_header(img): return hdr - -def patch_header(img): - """ - Patch the image with the new header size and with the new image size. - """ - image_size = os.path.getsize(img + ".elf.bin") - print("Image size (bin): %d" % image_size) - - try: - with open(img + ".img", "r+b") as f: - print("[*] Loading : {0}".format(img)) - - # Patch the header size (Hardcoded to 0x80) - f.seek(OFFSET_IH_HDR_SIZE) - f.write('\x80\x00') - - # Patch the image size, start by convert int to hex string. - image_size_hex = struct.pack('i', image_size) - f.seek(OFFSET_IH_IMG_SIZE) - f.write(image_size_hex) - - f.close() - except IOError: - print("[*] Cannot open {0} (!) ".format(img)) - - return image_size - - -def extend_image(img, img_size): +def write_partial_img(image_name, hdr): try: - # Read the header and image locally - with open(img + ".img", "rb") as f: - print("[*] Loading %s (img_size: %d)" % (img, img_size)) - hdr = f.read(IMAGE_HEADER_SIZE) - image = f.read(img_size) + with open(image_name + ".elf.bin", "rb") as f: + image = f.read() f.close() + print("image len: %d" % len(image)) - # Clear the file (why doesn't truncate work?) - open(img + ".img").close() - - # Store the header, pad it and then store the image itself - with open(img + ".img", "w+b") as f: + print("[*] Writing : {0}".format(image_name)) + with open(image_name + ".img", "w+b") as f: f.write(hdr) - # Pad the header up til where the image itself begins - f.write(('\xFF' * (OFFSET_VECTOR_TABLE - IMAGE_HEADER_SIZE))) + f.write('\xFF' * (OFFSET_VECTOR_TABLE - IMAGE_HEADER_SIZE)) f.write(image) f.close() - except IOError: - print("[*] Cannot open {0} (!) ".format(img)) + except IOError: + print("[*] Cannot open {0} (!) ".format(image_name)) -def calculate_hash(img): +def calculate_hash(image_name): sha256 = hashlib.sha256() - with open(img + ".img", "rb") as f: + with open(image_name + ".img", "rb") as f: sha256.update(f.read()) return sha256.hexdigest() @@ -113,10 +79,8 @@ def append_hash(img, digest): def main(img): # backup(img) # Backup original image - create_header(img) - size = patch_header(img) - extend_image(img, size) - + hdr = create_header(img) + write_partial_img(img, hdr) digest = calculate_hash(img) print("Hash of intermediate image: %s" % digest) |