Files
meshcore-tth/tools/ota_fixtures.py
T
tthandClaude Opus 5.5 d22005a6fd
pub-release / release (push) Successful in 32m57s
OTA phase 5: tools and docs ready for the public export
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 18:07:39 +01:00

103 lines
3.2 KiB
Python
Executable File

#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
"""Regenerate test/test_fwpatch/fixtures.h (in-place patches for the native test).
tools/ota_fixtures.py # writes test/test_fwpatch/fixtures.h
Needs detools (pip, in a venv; not on the CI runner -- that is why the result is
committed). The images themselves are NOT stored: test_fwpatch.cpp rebuilds them
with the same xorshift32 generator as make_images() below, so only the patches
(a few kB) live in the header. Change the generator on both sides or neither.
Cases:
grow new = old + 8 kB inserted early, memory tight enough that the
shifted old image and the new one overlap (the hard path)
shrink new = old - 3 kB, scattered edits
same new = old with 16 changed bytes (the "rebuild" case)
"""
import io
import pathlib
import sys
if any(a in ("-h", "--help") for a in sys.argv[1:]):
print(__doc__)
sys.exit(0)
import detools
OUT = pathlib.Path(__file__).resolve().parent.parent / "test/test_fwpatch/fixtures.h"
PAGE = 4096
def xorshift(seed: int, n: int) -> bytes:
x = seed & 0xFFFFFFFF
out = bytearray(n)
for i in range(n):
x ^= (x << 13) & 0xFFFFFFFF
x ^= x >> 17
x ^= (x << 5) & 0xFFFFFFFF
out[i] = x & 0xFF
return bytes(out)
def make_images(case: str) -> tuple[bytes, bytes]:
old = xorshift(0x12345678, 60000)
if case == "grow":
new = old[:5000] + xorshift(0xCAFEBABE, 8192) + old[5000:]
elif case == "shrink":
b = bytearray(old[:20000] + old[23000:])
for i in range(0, len(b), 4093):
b[i] ^= 0x5A
new = bytes(b)
else: # same
b = bytearray(old)
for i in range(16):
b[1000 + i * 3001] ^= 0xFF
new = bytes(b)
return old, new
MEMORY = {"grow": 30 * PAGE, "shrink": 32 * PAGE, "same": 40 * PAGE}
def main() -> int:
lines = [
"// GENERATED by tools/ota_fixtures.py -- do not edit.",
"#pragma once",
"#include <stdint.h>",
"",
]
for case, mem in MEMORY.items():
old, new = make_images(case)
p = io.BytesIO()
detools.create_patch(
io.BytesIO(old),
io.BytesIO(new),
p,
compression="heatshrink",
patch_type="in-place",
memory_size=mem,
segment_size=PAGE,
heatshrink_window_sz2=11,
heatshrink_lookahead_sz2=10,
)
data = p.getvalue()
lines.append(f"// {case}: old {len(old)} B, new {len(new)} B, memory {mem} B")
lines.append(f"static const uint32_t fx_{case}_mem = {mem};")
lines.append(f"static const uint32_t fx_{case}_old = {len(old)};")
lines.append(f"static const uint32_t fx_{case}_new = {len(new)};")
lines.append(f"static const uint8_t fx_{case}_patch[{len(data)}] = {{")
for i in range(0, len(data), 16):
lines.append(" " + ", ".join(f"0x{b:02x}" for b in data[i : i + 16]) + ",")
lines.append("};")
lines.append("")
print(f"{case}: patch {len(data)} B")
OUT.write_text("\n".join(lines))
print(f"wrote {OUT}")
return 0
if __name__ == "__main__":
sys.exit(main())