229 lines
6.8 KiB
Python
Executable File
229 lines
6.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
Harman Kardon Aura (1st Gen) Firmware Region Patcher
|
|
|
|
This script patches the region/country code in HK Aura firmware files.
|
|
The US firmware limits WiFi to channels 1-11, which can cause connectivity
|
|
issues in Europe where routers may use channels 12-13.
|
|
|
|
Supported regions:
|
|
- US: Channels 1-11 (FCC)
|
|
- EU: Channels 1-13 (ETSI)
|
|
- JP: Channels 1-14 (TELEC)
|
|
|
|
Usage:
|
|
python patch_firmware.py <input.bin> <region> [output.bin]
|
|
|
|
Example:
|
|
python patch_firmware.py HK_AURA.bin EU HK_AURA_EU.bin
|
|
"""
|
|
|
|
import struct
|
|
import sys
|
|
import os
|
|
|
|
# Valid region codes and their descriptions
|
|
VALID_REGIONS = {
|
|
'US': 'USA (FCC) - Channels 1-11',
|
|
'EU': 'Europe (ETSI) - Channels 1-13',
|
|
'JP': 'Japan (TELEC) - Channels 1-14',
|
|
}
|
|
|
|
# Firmware constants
|
|
MAGIC = b'HUI '
|
|
COUNTRYCODE_OFFSET = 0x3233e # Offset of the 2-byte country code
|
|
COUNTRYCODE_PREFIX = b'CountryCode'
|
|
SEGMENT1_CHECKSUM_OFFSET = 0x40
|
|
SEGMENT1_START = 0x70
|
|
SEGMENT1_SIZE = 0x323d4
|
|
|
|
|
|
def calculate_checksum(data: bytes) -> int:
|
|
"""
|
|
Calculate the segment checksum.
|
|
Algorithm: Two's complement of the sum of all 32-bit little-endian words.
|
|
"""
|
|
add_sum = 0
|
|
for i in range(0, len(data), 4):
|
|
if i + 4 <= len(data):
|
|
word = struct.unpack('<I', data[i:i+4])[0]
|
|
else:
|
|
remaining = data[i:] + b'\x00' * (4 - len(data[i:]))
|
|
word = struct.unpack('<I', remaining)[0]
|
|
add_sum = (add_sum + word) & 0xffffffff
|
|
|
|
return ((~add_sum) + 1) & 0xffffffff
|
|
|
|
|
|
def validate_firmware(data: bytes) -> tuple[bool, str]:
|
|
"""Validate that this is a valid HK Aura firmware file."""
|
|
|
|
# Check magic header
|
|
if data[:4] != MAGIC:
|
|
return False, f"Invalid magic header. Expected 'HUI ', got '{data[:4]}'"
|
|
|
|
# Check for product name
|
|
if b'HK AURA' not in data[0x20:0x30]:
|
|
return False, "Product name 'HK AURA' not found in header"
|
|
|
|
# Check for CountryCode string
|
|
if COUNTRYCODE_PREFIX not in data[COUNTRYCODE_OFFSET-11:COUNTRYCODE_OFFSET]:
|
|
return False, f"CountryCode string not found at expected offset"
|
|
|
|
# Verify current segment checksum
|
|
segment_data = data[SEGMENT1_START:SEGMENT1_START + SEGMENT1_SIZE]
|
|
calculated = calculate_checksum(segment_data)
|
|
stored = struct.unpack('<I', data[SEGMENT1_CHECKSUM_OFFSET:SEGMENT1_CHECKSUM_OFFSET+4])[0]
|
|
|
|
if calculated != stored:
|
|
return False, f"Segment checksum mismatch. Calculated: {calculated:#010x}, Stored: {stored:#010x}"
|
|
|
|
return True, "Firmware validation passed"
|
|
|
|
|
|
def get_current_region(data: bytes) -> str:
|
|
"""Get the current region code from firmware."""
|
|
code = data[COUNTRYCODE_OFFSET:COUNTRYCODE_OFFSET+2].decode('ascii')
|
|
return code
|
|
|
|
|
|
def patch_firmware(data: bytes, new_region: str) -> bytes:
|
|
"""Patch the firmware with a new region code."""
|
|
|
|
if new_region not in VALID_REGIONS:
|
|
raise ValueError(f"Invalid region '{new_region}'. Valid regions: {', '.join(VALID_REGIONS.keys())}")
|
|
|
|
# Convert to bytearray for modification
|
|
patched = bytearray(data)
|
|
|
|
# Patch the country code
|
|
new_code = new_region.encode('ascii')
|
|
patched[COUNTRYCODE_OFFSET] = new_code[0]
|
|
patched[COUNTRYCODE_OFFSET + 1] = new_code[1]
|
|
|
|
# Recalculate segment checksum
|
|
segment_data = bytes(patched[SEGMENT1_START:SEGMENT1_START + SEGMENT1_SIZE])
|
|
new_checksum = calculate_checksum(segment_data)
|
|
|
|
# Update checksum in header
|
|
struct.pack_into('<I', patched, SEGMENT1_CHECKSUM_OFFSET, new_checksum)
|
|
|
|
return bytes(patched)
|
|
|
|
|
|
def main():
|
|
print("=" * 60)
|
|
print("Harman Kardon Aura (1st Gen) Firmware Region Patcher")
|
|
print("=" * 60)
|
|
print()
|
|
|
|
# Parse arguments
|
|
if len(sys.argv) < 3:
|
|
print("Usage: python patch_firmware.py <input.bin> <region> [output.bin]")
|
|
print()
|
|
print("Regions:")
|
|
for code, desc in VALID_REGIONS.items():
|
|
print(f" {code}: {desc}")
|
|
print()
|
|
print("Example:")
|
|
print(" python patch_firmware.py HK_AURA.bin EU HK_AURA_EU.bin")
|
|
sys.exit(1)
|
|
|
|
input_file = sys.argv[1]
|
|
new_region = sys.argv[2].upper()
|
|
|
|
if len(sys.argv) >= 4:
|
|
output_file = sys.argv[3]
|
|
else:
|
|
base, ext = os.path.splitext(input_file)
|
|
output_file = f"{base}_{new_region}{ext}"
|
|
|
|
# Validate region
|
|
if new_region not in VALID_REGIONS:
|
|
print(f"Error: Invalid region '{new_region}'")
|
|
print(f"Valid regions: {', '.join(VALID_REGIONS.keys())}")
|
|
sys.exit(1)
|
|
|
|
# Read input file
|
|
print(f"Reading: {input_file}")
|
|
try:
|
|
with open(input_file, 'rb') as f:
|
|
data = f.read()
|
|
except FileNotFoundError:
|
|
print(f"Error: File not found: {input_file}")
|
|
sys.exit(1)
|
|
|
|
print(f"File size: {len(data):,} bytes")
|
|
print()
|
|
|
|
# Validate firmware
|
|
print("Validating firmware...")
|
|
valid, message = validate_firmware(data)
|
|
if not valid:
|
|
print(f"Error: {message}")
|
|
sys.exit(1)
|
|
print(f" {message}")
|
|
print()
|
|
|
|
# Get current region
|
|
current_region = get_current_region(data)
|
|
print(f"Current region: {current_region}", end="")
|
|
if current_region in VALID_REGIONS:
|
|
print(f" ({VALID_REGIONS[current_region]})")
|
|
else:
|
|
print(f" (unknown)")
|
|
|
|
print(f"New region: {new_region} ({VALID_REGIONS[new_region]})")
|
|
print()
|
|
|
|
if current_region == new_region:
|
|
print("Firmware already has the requested region. No changes needed.")
|
|
sys.exit(0)
|
|
|
|
# Patch firmware
|
|
print("Patching firmware...")
|
|
patched_data = patch_firmware(data, new_region)
|
|
|
|
# Verify patch
|
|
print("Verifying patch...")
|
|
patched_region = get_current_region(patched_data)
|
|
if patched_region != new_region:
|
|
print(f"Error: Patch verification failed. Region is '{patched_region}' instead of '{new_region}'")
|
|
sys.exit(1)
|
|
|
|
# Verify checksum
|
|
segment_data = patched_data[SEGMENT1_START:SEGMENT1_START + SEGMENT1_SIZE]
|
|
calculated = calculate_checksum(segment_data)
|
|
stored = struct.unpack('<I', patched_data[SEGMENT1_CHECKSUM_OFFSET:SEGMENT1_CHECKSUM_OFFSET+4])[0]
|
|
|
|
if calculated != stored:
|
|
print(f"Error: Checksum verification failed")
|
|
sys.exit(1)
|
|
|
|
print(f" Region: {patched_region}")
|
|
print(f" Checksum: {stored:#010x} (valid)")
|
|
print()
|
|
|
|
# Write output file
|
|
print(f"Writing: {output_file}")
|
|
with open(output_file, 'wb') as f:
|
|
f.write(patched_data)
|
|
|
|
print()
|
|
print("=" * 60)
|
|
print("SUCCESS!")
|
|
print("=" * 60)
|
|
print()
|
|
print(f"Patched firmware saved to: {output_file}")
|
|
print()
|
|
print("To flash the firmware:")
|
|
print(" 1. Copy the file to a FAT32 formatted USB drive")
|
|
print(" 2. Connect the USB drive to your HK Aura")
|
|
print(" 3. Follow the standard firmware update procedure")
|
|
print()
|
|
print("WARNING: Flashing modified firmware is at your own risk!")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|