Skip to content

Instantly share code, notes, and snippets.

@skgsergio
Created July 17, 2024 18:21
Show Gist options
  • Save skgsergio/f64af53ba00a42814f772f5285447fc2 to your computer and use it in GitHub Desktop.
Save skgsergio/f64af53ba00a42814f772f5285447fc2 to your computer and use it in GitHub Desktop.
import sys
import pathlib
from zigpy.ota.image import parse_ota_image
if len(sys.argv) != 4:
print(f"Usage: {sys.argv[0]} ota_file new_version_hex_or_int output_dir")
sys.exit(1)
ota_file = pathlib.Path(sys.argv[1])
new_version = int(sys.argv[2], 16) if sys.argv[2].startswith("0x") else int(sys.argv[2])
output_ota_file = pathlib.Path(sys.argv[3]) / f"0x{new_version:0>8X}_{ota_file.name}"
print("Reading OTA image...")
image, rem = parse_ota_image(ota_file.read_bytes())
if rem:
print("There is more data in the image after parsing the ota, aborting!")
sys.exit(1)
print("Patching OTA version...")
print(f" Image version: 0x{image.header.file_version:0>8X} ({image.header.file_version})")
print(f" Patch version: 0x{new_version:0>8X} ({new_version})")
image.header.file_version = new_version
print(f"Saving patched OTA as {output_ota_file}...")
output_ota_file.parent.mkdir(parents=True, exist_ok=True)
output_ota_file.write_bytes(image.serialize())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment