Files
hk-aura/tools/patch_debuglog.py

79 lines
2.7 KiB
Python

#!/usr/bin/env python3
"""
Experimental HK Aura debug-log-over-SPP patch.
Two changes:
1. FUN_0801F16E only sends AA/BB frames when channel[7] == 1. That flag is
never set on this unit, so every SPP reply and log frame is dropped. Patch
the two `bne` instructions to `nop` to ignore channel[7].
2. FUN_08011F08 / FUN_08011F34 normally write formatted logs to UART via
FUN_080202C8. Patch both output calls to FUN_0801F16E so logs go over the
RN52/SPP link instead.
The resulting AA/BB frames can be read by `aura_wifi_bt.py --framed-listen`.
Patch table:
0x0801F18E 10 d1 -> 00 bf
0x0801F1C8 10 d1 -> 00 bf
0x08011F28 0e f0 ce f9 -> 0d f0 21 f9 (bl 0x0801F16E)
0x08011F54 0e f0 b8 f9 -> 0d f0 0b f9 (bl 0x0801F16E)
Usage:
python3 tools/patch_debuglog.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_debuglog.bin'
SEG1_START = 0x70
SEG1_SIZE = 0x323D4
SEG1_CHECKSUM_OFFSET = 0x40
PATCHES = [
(0x0801F18E, bytes.fromhex('10 d1'), bytes.fromhex('00 bf')),
(0x0801F1C8, bytes.fromhex('10 d1'), bytes.fromhex('00 bf')),
(0x08011F28, bytes.fromhex('0e f0 ce f9'), bytes.fromhex('0d f0 21 f9')),
(0x08011F54, bytes.fromhex('0e f0 b8 f9'), bytes.fromhex('0d f0 0b f9')),
]
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}')
for addr, want, repl in PATCHES:
off = addr - 0x0800C000 + SEG1_START
if data[off:off + len(want)] != want:
sys.exit(f'unexpected bytes at {addr:#x} (file {off:#x}): '
f'{data[off:off + len(want)].hex(" ")}')
data[off:off + len(repl)] = repl
print(f'patched {addr:#x} (file {off:#x}): {want.hex(" ")} -> {repl.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()