Rough out being able to merge image files & firmware files

This commit is contained in:
Ben V. Brown
2024-08-20 07:56:48 +10:00
parent 29e96bb25b
commit 97756fe21c

View File

@@ -4,7 +4,7 @@ from __future__ import division
import argparse import argparse
import copy import copy
import os, sys import os, sys
from intelhex import IntelHex
from output_hex import HexOutput from output_hex import HexOutput
from output_dfu import DFUOutput from output_dfu import DFUOutput
@@ -243,6 +243,7 @@ def animated_image_to_bytes(
def img2hex( def img2hex(
input_filename, input_filename,
device_model_name: str, device_model_name: str,
merge_hex_file: str | None,
preview_filename=None, preview_filename=None,
threshold=128, threshold=128,
dither=False, dither=False,
@@ -323,6 +324,10 @@ def img2hex(
split_name = [base, ext] split_name = [base, ext]
output_name = output_filename_base + split_name[0] + split_name[1] output_name = output_filename_base + split_name[0] + split_name[1]
# If a file has been specified for merging, we want to splice our image data with it
if merge_hex_file is not None:
read_merge_write(merge_hex_file, data, deviceSettings, output_name)
else:
DFUOutput.writeFile( DFUOutput.writeFile(
output_name + ".dfu", output_name + ".dfu",
data, data,
@@ -341,6 +346,42 @@ def img2hex(
) )
def read_merge_write(
merge_filename: str, image_data: list[int], deviceSettings, output_filename: str
):
"""
Reads in the merge filename as the base object, then inserts the image data.
Then pad-fills the empty space in the binary
"""
base_hex_file = IntelHex(merge_filename)
logo_hex_file = IntelHex()
logo_hex_file.frombytes(image_data, deviceSettings.IMAGE_ADDRESS)
# Merge in the image data, error if collision
base_hex_file.merge(logo_hex_file, overlap="error")
binary_base = base_hex_file.minaddr()
base_hex_file.padding = 0xA5
binary_blob = base_hex_file.tobinarray(start=binary_base)
print(
f"Post-merge output image starts at 0x{binary_base:x}, len {len(binary_blob)}"
)
DFUOutput.writeFile(
output_filename + ".dfu",
binary_blob,
binary_base,
deviceSettings.DFU_TARGET_NAME,
deviceSettings.DFU_ALT,
deviceSettings.DFU_PRODUCT,
deviceSettings.DFU_VENDOR,
)
HexOutput.writeFile(
output_filename + ".hex",
binary_blob,
binary_base,
deviceSettings.MINIMUM_HEX_SIZE,
)
def parse_commandline(): def parse_commandline():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter, formatter_class=argparse.ArgumentDefaultsHelpFormatter,
@@ -363,6 +404,12 @@ def parse_commandline():
help="filename of image preview", help="filename of image preview",
) )
parser.add_argument(
"-M",
"--merge",
help="filename of another hex file to merge with, creating a combined firmware",
)
parser.add_argument( parser.add_argument(
"-n", "-n",
"--negative", "--negative",
@@ -420,6 +467,7 @@ if __name__ == "__main__":
print(f"Converting {args.input_filename} => {args.output_filename}") print(f"Converting {args.input_filename} => {args.output_filename}")
img2hex( img2hex(
merge_hex_file=args.merge,
input_filename=args.input_filename, input_filename=args.input_filename,
output_filename_base=args.output_filename, output_filename_base=args.output_filename,
device_model_name=args.model, device_model_name=args.model,
@@ -432,6 +480,7 @@ if __name__ == "__main__":
) )
img2hex( img2hex(
merge_hex_file=args.merge,
input_filename=args.input_filename, input_filename=args.input_filename,
output_filename_base=args.output_filename, output_filename_base=args.output_filename,
device_model_name=args.model, device_model_name=args.model,