Files
hk-aura/tools/patch_led_probe.py

71 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""
Experimental HK Aura LED-probe patch.
The `00 01` (query capability) branch of the SPP dispatcher calls
FUN_08019000. This patch replaces that call with FUN_0801E570, the LED
spot-step animation function found in the MCU firmware. Each `00 01` frame
then advances the 30-LED ring spot by one step instead of sending a
capability reply.
Target:
0x080198BE BL FUN_08019000 -> BL FUN_0801E570
bytes: ff f7 9f fb -> 04 f0 57 fe
Build it on top of firmware/HK_AURA_EU_spp_data_mode.bin (EU region + the
SPP data-mode patch), or pass another base image.
Usage:
python3 tools/patch_led_probe.py [input.bin] [output.bin]
"""
import hashlib
import struct
import sys
SRC = 'firmware/HK_AURA_EU_spp_data_mode.bin'
OUT = 'firmware/HK_AURA_EU_ledprobe.bin'
SEG1_START = 0x70
SEG1_SIZE = 0x323D4
SEG1_CHECKSUM_OFFSET = 0x40
ADDR = 0x080198BE
OLD = bytes.fromhex('ff f7 9f fb')
NEW = bytes.fromhex('04 f0 57 fe')
def checksum(data):
total = 0
for i in range(0, len(data), 4):
total = (total + struct.unpack('<I', data[i:i + 4])[0]) & 0xFFFFFFFF
return ((~total) + 1) & 0xFFFFFFFF
def main():
src = sys.argv[1] if len(sys.argv) > 1 else SRC
out = sys.argv[2] if len(sys.argv) > 2 else OUT
data = bytearray(open(src, 'rb').read())
if data[:4] != b'HUI ':
sys.exit('not a HUI firmware image')
old = struct.unpack('<I', data[SEG1_CHECKSUM_OFFSET:SEG1_CHECKSUM_OFFSET + 4])[0]
calc = checksum(bytes(data[SEG1_START:SEG1_START + SEG1_SIZE]))
if old != calc:
sys.exit(f'input segment checksum mismatch: stored {old:#x}, calc {calc:#x}')
off = ADDR - 0x0800C000 + SEG1_START
if data[off:off + 4] != OLD:
sys.exit(f'unexpected bytes at {ADDR:#x} (file {off:#x}): '
f'{data[off:off + 4].hex(" ")}')
data[off:off + 4] = NEW
print(f'patched {ADDR:#x} (file {off:#x}): {OLD.hex(" ")} -> {NEW.hex(" ")}')
new = checksum(bytes(data[SEG1_START:SEG1_START + SEG1_SIZE]))
struct.pack_into('<I', data, SEG1_CHECKSUM_OFFSET, new)
open(out, 'wb').write(data)
print(f'segment checksum: {old:#x} -> {new:#x}')
print(f'output: {out}')
print(f'sha256: {hashlib.sha256(data).hexdigest()}')
if __name__ == '__main__':
main()