Files
hk-aura/tools/patch_spp_data_mode.py

75 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""
Experimental HK Aura SPP data-mode patch.
The MCU SPP connection handler FUN_08017512 sets the BT channel mode byte
(channel[8]) to 1 (command mode) when an SPP connection event arrives, and never
sets it back to 0 (data mode). The BT task loop (FUN_0801f6b2) then calls
FUN_0802535c (command parser) instead of FUN_080252f0 (SPP parser), so the phone's
03 01 / 03 04 frames never reach the HarmanSpp dispatcher.
This script changes the two "movs r1,#1" instructions immediately before the
"strb r1,[r0,#8]" mode stores to "movs r1,#0", leaving the channel in data mode.
It recalculates the segment 1 checksum. Nothing else is changed.
Targets:
0x0801753C -> file 0xB5AC (channel 1)
0x080175BC -> file 0xB62C (channel 2)
Usage:
python3 tools/patch_spp_data_mode.py [input.bin] [output.bin]
"""
import hashlib
import struct
import sys
SRC = 'firmware/HK_AURA_US_original.bin'
OUT = 'firmware/HK_AURA_US_spp_data_mode.bin'
SEG1_START = 0x70
SEG1_SIZE = 0x323D4
SEG1_CHECKSUM_OFFSET = 0x40
PATCHES = [
(0x0801753C, bytes.fromhex('01 21'), bytes.fromhex('00 21')),
(0x080175BC, bytes.fromhex('01 21'), bytes.fromhex('00 21')),
]
def checksum(data):
total = 0
for i in range(0, len(data), 4):
word = struct.unpack('<I', data[i:i + 4])[0]
total = (total + word) & 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 + 2] != want:
sys.exit(f'unexpected bytes at {addr:#x} (file {off:#x}): '
f'{data[off:off + 2].hex(" ")}')
data[off:off + 2] = 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()