python-validity/proto9x/upload_fwext.py
Viktor Dragomiretskyy c60d74027b Don't break 0097
2020-06-24 04:24:12 +12:00

62 lines
2 KiB
Python

from binascii import hexlify, unhexlify
from time import ctime
from os.path import basename
from .tls import tls
from .usb import usb
from .sensor import reboot, write_hw_reg32, read_hw_reg32, identify_sensor
from .flash import flush_changes, read_flash, erase_flash, write_flash_all, write_fw_signature, get_fw_info
from .util import assert_status
def default_fwext_name():
if usb.usb_dev().idVendor == 0x138a:
if usb.usb_dev().idProduct == 0x0090:
return '6_07f_Lenovo.xpfwext'
elif usb.usb_dev().idProduct == 0x0097:
return '6_07f_lenovo_mis.xpfwext'
elif usb.usb_dev().idVendor == 0x06cb:
if usb.usb_dev().idProduct == 0x009a:
return '6_07f_lenovo_mis_qm.xpfwext'
def upload_fwext(fw_path=None):
# no idea what this is:
write_hw_reg32(0x8000205c, 7)
if read_hw_reg32(0x80002080) not in [2, 3]:
raise Exception('Unexpected register value')
dev=identify_sensor()
print('Sensor: %s' % dev.name)
# ^ TODO -- what is the real reason to detect HW at this stage?
# just a guess: perhaps it is used to construct fwext filename
default_name = default_fwext_name()
if not fw_path:
fw_path = default_name
elif basename(fw_path) != default_name:
print('WARNING: Your device fw is supposed to be called {}'.format(
default_name))
with open(fw_path, 'rb') as f:
fwext=f.read()
fwext=fwext[fwext.index(b'\x1a')+1:]
fwext, signature = fwext[:-0x100], fwext[-0x100:]
fwi=get_fw_info(2)
if fwi != None:
raise Exception('FW is already present (version %d.%d (%s))' % (fwi.major, fwi.minor, ctime(fwi.buildtime)))
#flush_changes()
write_flash_all(2, 0, fwext)
write_fw_signature(2, signature)
fwi=get_fw_info(2)
if fwi == None:
raise Exception('No firmware detected')
print('Loaded FWExt version %d.%d (%s), %d modules' % (fwi.major, fwi.minor, ctime(fwi.buildtime), len(fwi.modules)))
# Reboot
reboot()