| 1 |
#!/usr/bin/env python3 |
| 2 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 3 |
# Copyright (C) 2018 Takashi Sakamoto |
| 4 |
|
| 5 |
import sys |
| 6 |
import errno |
| 7 |
from pprint import PrettyPrinter |
| 8 |
from struct import unpack |
| 9 |
|
| 10 |
from hinawa_utils.ieee1212.config_rom_lexer import Ieee1212ConfigRomLexer |
| 11 |
from hinawa_utils.ieee1394.config_rom_parser import Ieee1394ConfigRomParser |
| 12 |
|
| 13 |
import gi |
| 14 |
gi.require_version('Hinawa', '4.0') |
| 15 |
from gi.repository import Hinawa |
| 16 |
|
| 17 |
if len(sys.argv) < 2: |
| 18 |
print('At least one argument is required for firewire character device.') |
| 19 |
sys.exit(errno.EINVAL) |
| 20 |
path = sys.argv[1] |
| 21 |
|
| 22 |
ops = ('parse', 'lex') |
| 23 |
if len(sys.argv) < 3: |
| 24 |
op = 'parse' |
| 25 |
elif sys.argv[2] in ops: |
| 26 |
op = sys.argv[2] |
| 27 |
else: |
| 28 |
print('Invalid operation: {0}'.format(sys.argv[2])) |
| 29 |
sys.exit() |
| 30 |
|
| 31 |
# For Echo Audio Fireworks series. |
| 32 |
|
| 33 |
|
| 34 |
def handle_echoaudio_keys(key_id, type_name, data): |
| 35 |
OUI_COMPANIES = { |
| 36 |
0x000ff2: 'Loud Technologies Inc.', |
| 37 |
0x001486: 'Echo Digital Audio Corporation', |
| 38 |
} |
| 39 |
if key_id == 0x08 and type_name == 'IMMEDIATE': |
| 40 |
if data in OUI_COMPANIES: |
| 41 |
name = OUI_COMPANIES[data] |
| 42 |
else: |
| 43 |
name = data |
| 44 |
return ['MANUFACTURER', name] |
| 45 |
|
| 46 |
return None |
| 47 |
|
| 48 |
# For Apple iSight. |
| 49 |
|
| 50 |
|
| 51 |
def handle_iidc_v1_30_keys(key_id, type_name, data): |
| 52 |
if key_id == 0x00 and type_name == 'CSR_OFFSET': |
| 53 |
return ['IIDC_COMMAND_REGS_BASE', data] |
| 54 |
return None |
| 55 |
|
| 56 |
|
| 57 |
def handle_isight_audio_keys(key_id, type_name, data): |
| 58 |
if key_id == 0x00 and type_name == 'CSR_OFFSET': |
| 59 |
return ['ISIGHT_AUDIO_REG_BASE', data] |
| 60 |
return None |
| 61 |
|
| 62 |
|
| 63 |
def handle_isight_factory_keys(key_id, type_name, data): |
| 64 |
LABELS = { |
| 65 |
0x01: 'ISIGHT_FACTORY_REG_A', |
| 66 |
0x02: 'ISIGHT_FACTORY_REG_B', |
| 67 |
0x04: 'ISIGHT_FACTORY_REG_C', |
| 68 |
0x05: 'ISIGHT_FACTORY_REG_D', |
| 69 |
} |
| 70 |
if type_name == 'CSR_OFFSET' and key_id in LABELS: |
| 71 |
return [LABELS[key_id], data] |
| 72 |
elif key_id == 0x38 and type_name == 'IMMEDIATE': |
| 73 |
return ['ISIGHT_FACTORY_DATA', data] |
| 74 |
return None |
| 75 |
|
| 76 |
|
| 77 |
def handle_isight_iris_keys(key_id, type_name, data): |
| 78 |
if key_id == 0x3c and type_name == 'IMMEDIATE': |
| 79 |
return ['ISIGHT_IRIS_DATA', data] |
| 80 |
elif key_id == 0x00 and type_name == 'CSR_OFFSET': |
| 81 |
return ['ISIGHT_IRIS_REGS_BASE', data] |
| 82 |
return None |
| 83 |
|
| 84 |
# For TASCAM FireWire series. |
| 85 |
|
| 86 |
|
| 87 |
def handle_teac_keys(key_id, type_name, data): |
| 88 |
if key_id == 0x02 and type_name == 'LEAF': |
| 89 |
content = data[8:].decode('US-ASCII') + '\0' |
| 90 |
return ['MODEL_NAME', content[:content.find('\0')]] |
| 91 |
return None |
| 92 |
|
| 93 |
# For Digidesign Digi 00x series. |
| 94 |
|
| 95 |
|
| 96 |
def handle_microsoft_keys(key_id, type_name, data): |
| 97 |
if key_id == 0x01 and type_name == 'LEAF': |
| 98 |
width = data[0] >> 4 |
| 99 |
character_set = ((data[0] & 0x0f) << 8) | data[1] |
| 100 |
language = unpack('>H', data[2:4])[0] |
| 101 |
|
| 102 |
if character_set != 0x00 or language != 0x00: |
| 103 |
raise ValueError('Invalid data in descriptor leaf.') |
| 104 |
|
| 105 |
content = data[4:].decode('US-ASCII') + '\0' |
| 106 |
return content[:content.find('\0')] |
| 107 |
|
| 108 |
return None |
| 109 |
|
| 110 |
# For Cool Stream iSweet. |
| 111 |
|
| 112 |
|
| 113 |
def handle_ame_root_keys(key_id, type_name, data): |
| 114 |
if key_id == 0x06 and type_name == 'IMMEDIATE': |
| 115 |
return ['DEVICE_ID', data] |
| 116 |
return None |
| 117 |
|
| 118 |
|
| 119 |
def handle_iidc_v1_04_keys(key_id, type_name, data): |
| 120 |
if key_id == 0x00 and type_name == 'CSR_OFFSET': |
| 121 |
return ['COMMAND_REGS_BASE', data] |
| 122 |
return None |
| 123 |
|
| 124 |
|
| 125 |
def handle_ame_unit_dep_keys(key_id, type_name, data): |
| 126 |
if key_id == 0x02 and type_name == 'LEAF': |
| 127 |
descriptor_type = data[0] |
| 128 |
specifier_id = (data[1] << 16) | unpack('>H', data[2:4])[0] |
| 129 |
if descriptor_type != 0x00 or specifier_id != 0x00: |
| 130 |
raise ValueError('Invalid data in vendor-dep descriptor leaf.') |
| 131 |
|
| 132 |
width = data[4] >> 4 |
| 133 |
character_set = ((data[4] & 0x0f) << 8) | data[5] |
| 134 |
language = unpack('>H', data[6:8])[0] |
| 135 |
if character_set != 0x00 or language != 0x00: |
| 136 |
raise ValueError('Invalid data in descriptor leaf.') |
| 137 |
|
| 138 |
content = data[8:].decode('US-ASCII') + '\0' |
| 139 |
return ['MODEL_NAME', content[:content.find('\0')]] |
| 140 |
return None |
| 141 |
|
| 142 |
|
| 143 |
pp = PrettyPrinter(indent=2, compact=False) |
| 144 |
|
| 145 |
node = Hinawa.FwNode() |
| 146 |
node.open(path, 0) |
| 147 |
|
| 148 |
_, data = node.get_config_rom() |
| 149 |
|
| 150 |
if op == 'lex': |
| 151 |
entries = Ieee1212ConfigRomLexer.detect_entries(data) |
| 152 |
pp.pprint(entries) |
| 153 |
else: |
| 154 |
parser = Ieee1394ConfigRomParser() |
| 155 |
parser.add_vendor_dep_handle(0x001486, handle_echoaudio_keys) |
| 156 |
parser.add_spec_dep_handle(0x00a02d, 0x000102, handle_iidc_v1_30_keys) |
| 157 |
parser.add_spec_dep_handle(0x000a27, 0x000010, handle_isight_audio_keys) |
| 158 |
parser.add_spec_dep_handle(0x000a27, 0x000011, handle_isight_factory_keys) |
| 159 |
parser.add_spec_dep_handle(0x000a27, 0x000012, handle_isight_iris_keys) |
| 160 |
parser.add_spec_dep_handle(0x00022e, 0x800000, handle_teac_keys) |
| 161 |
parser.add_spec_dep_handle(0x00022e, 0x800003, handle_teac_keys) |
| 162 |
parser.add_spec_dep_handle(0x00022e, 0x800004, handle_teac_keys) |
| 163 |
parser.add_vendor_dep_handle(0x0050f2, handle_microsoft_keys) |
| 164 |
parser.add_spec_dep_handle(0x00a02d, 0x000100, handle_iidc_v1_04_keys) |
| 165 |
parser.add_spec_dep_handle(0x00a02d, 0x000100, handle_ame_unit_dep_keys) |
| 166 |
parser.add_vendor_dep_handle(0x0002f0, handle_ame_root_keys) |
| 167 |
info = parser.parse_rom(data) |
| 168 |
pp.pprint(info) |