Fix scripts

This commit is contained in:
Viktor Dragomiretskyy 2020-07-11 01:16:42 +12:00
parent 23498ba122
commit e33d4e956e
3 changed files with 39 additions and 85 deletions

View file

@ -6,51 +6,21 @@ from binascii import unhexlify
from enum import Enum from enum import Enum
from validitysensor.flash import read_flash from validitysensor.flash import read_flash
from validitysensor.sensor import * from validitysensor.sensor import *
from validitysensor.tls import tls as vfs_tls from validitysensor import init
from validitysensor.usb import usb as vfs_usb
from time import sleep from time import sleep
from usb import core as usb_core from usb import core as usb_core
class VFS(Enum):
DEV_90 = 0x0090
DEV_97 = 0x0097
if __name__ == "__main__": if __name__ == "__main__":
if os.geteuid() != 0: if os.geteuid() != 0:
raise Exception('This script needs to be executed as root') raise Exception('This script needs to be executed as root')
usb_dev = None init.open()
for d in VFS:
dev = usb_core.find(idVendor=0x138a, idProduct=d.value)
if dev:
usb_dev = dev
if not usb_dev:
raise Exception('No supported validity device found')
vfs_usb.open(product=usb_dev.idProduct)
vfs_usb.send_init()
try:
with open('/sys/class/dmi/id/product_name', 'r') as node:
product_name = node.read().strip()
with open('/sys/class/dmi/id/product_serial', 'r') as node:
product_serial = node.read().strip()
except:
product_name = 'VirtualBox'
product_serial = '0'
vfs_tls.set_hwkey(product_name=product_name, serial_number=product_serial)
# try to init TLS session from the flash
vfs_tls.parseTlsFlash(read_flash(1, 0, 0x1000))
vfs_tls.open()
for i in range(0, 10): for i in range(0, 10):
glow_start_scan() glow_start_scan()
sleep(0.05) sleep(0.05)
glow_end_enroll() glow_end_scan()
sleep(0.05) sleep(0.05)
led_script = unhexlify( led_script = unhexlify(

View file

@ -24,13 +24,14 @@ import subprocess
import sys import sys
import tempfile import tempfile
import urllib.request import urllib.request
import shutil
from enum import Enum, auto from enum import Enum, auto
from time import sleep from time import sleep
from usb import core as usb_core from usb import core as usb_core
from validitysensor.flash import read_flash from validitysensor.flash import read_tls_flash
from validitysensor.init_db import init_db from validitysensor.init_db import init_db
from validitysensor.init_flash import init_flash from validitysensor.init_flash import init_flash
from validitysensor.sensor import sensor as vfs_sensor from validitysensor.sensor import sensor as vfs_sensor
@ -50,8 +51,8 @@ DEFAULT_URIS = {
'referral': 'https://support.lenovo.com/us/en/downloads/DS120491', 'referral': 'https://support.lenovo.com/us/en/downloads/DS120491',
}, },
VFS.DEV_97: { VFS.DEV_97: {
'driver': 'https://download.lenovo.com/pccbbs/mobiles/n1mgf03w.exe', 'driver': 'https://download.lenovo.com/pccbbs/mobiles/nz3gf07w.exe',
'referral': 'https://download.lenovo.com/pccbbs/mobiles/n1mgf03w.exe' 'referral': 'https://download.lenovo.com/pccbbs/mobiles/nz3gf07w.exe'
}, },
VFS.DEV_9a: { VFS.DEV_9a: {
'driver': 'https://download.lenovo.com/pccbbs/mobiles/nz3gf07w.exe', 'driver': 'https://download.lenovo.com/pccbbs/mobiles/nz3gf07w.exe',
@ -61,7 +62,7 @@ DEFAULT_URIS = {
DEFAULT_FW_NAMES = { DEFAULT_FW_NAMES = {
VFS.DEV_90: '6_07f_Lenovo.xpfwext', VFS.DEV_90: '6_07f_Lenovo.xpfwext',
VFS.DEV_97: '6_07f_lenovo_mis.xpfwext', VFS.DEV_97: '6_07f_lenovo_mis_qm.xpfwext',
VFS.DEV_9a: '6_07f_lenovo_mis_qm.xpfwext' VFS.DEV_9a: '6_07f_lenovo_mis_qm.xpfwext'
} }
@ -75,26 +76,9 @@ class VFSInitializer():
print('Found device {}'.format(self.dev_str)) print('Found device {}'.format(self.dev_str))
try: if self.args.host_product or self.args.host_serial:
if self.args.simulate_virtualbox: vfs_tls.set_hwkey(product_name=self.args.host_product,
raise(Exception()) serial_number=self.args.host_serial)
with open('/sys/class/dmi/id/product_name', 'r') as node:
self.product_name = node.read().strip()
with open('/sys/class/dmi/id/product_serial', 'r') as node:
self.product_serial = node.read().strip()
except:
self.product_name = 'VirtualBox'
self.product_serial = '0'
if self.args.host_product:
self.product_name = self.args.host_product
if self.args.host_serial:
self.product_serial = self.args.host_serial
vfs_tls.set_hwkey(product_name=self.product_name,
serial_number=self.product_serial)
def open_device(self, init=False): def open_device(self, init=False):
print('Opening device',hex(self.dev_type.value[1])) print('Opening device',hex(self.dev_type.value[1]))
@ -104,7 +88,7 @@ class VFSInitializer():
vfs_usb.send_init() vfs_usb.send_init()
# try to init TLS session from the flash # try to init TLS session from the flash
vfs_tls.parseTlsFlash(read_flash(1, 0, 0x1000)) vfs_tls.parseTlsFlash(read_tls_flash())
vfs_tls.open() vfs_tls.open()
def restart(self): def restart(self):
@ -155,10 +139,11 @@ class VFSInitializer():
print('Factory reset failed with {}, this should not happen, but ' \ print('Factory reset failed with {}, this should not happen, but ' \
'we can ignore it, if pairing works...'.format(e)) 'we can ignore it, if pairing works...'.format(e))
def pair(self, fwpath): def pair(self):
print('Pairing the sensor with device {}'.format(self.product_name)) print('Pairing the sensor with device')
max_retries = 5 max_retries = 5
err = None
for i in range(0, max_retries): for i in range(0, max_retries):
try: try:
self.open_device() self.open_device()
@ -167,7 +152,8 @@ class VFSInitializer():
init_flash() init_flash()
break break
except Exception as e: except Exception as e:
err = e if err is None:
err = e
self.sleep() self.sleep()
print('Try {} failed with error: {}'.format(i+1, e)) print('Try {} failed with error: {}'.format(i+1, e))
finally: finally:
@ -181,29 +167,14 @@ class VFSInitializer():
self.restart() self.restart()
print('Uploading firmware...') print('Uploading firmware...')
upload_fwext(fw_path=fwpath) upload_fwext()
self.sleep() self.sleep()
self.restart() self.restart()
if self.args.calibration_data: print('Calibrating...')
calib_data_file = self.args.calibration_data.name
else:
calib_data_file = 'calib-data.bin'
print('Calibrating, re-using {}, if any...'.format(calib_data_file))
vfs_sensor.open() vfs_sensor.open()
if os.path.exists(calib_data_file): vfs_sensor.calibrate()
vfs_sensor.calibrate()
else:
try:
calib_data_file = os.path.join(tempfile.mkdtemp(), 'calib-data.bin')
vfs_sensor.calibrate()
print('Calibration data saved at {}'.format(calib_data_file))
except Exception as e:
print('Calibration failed using device data ({}), ' \
'You can try loading a local `calib-data.bin` blob, ' \
'the device should work anyway, so skipping...'.format(e))
print('Init database...') print('Init database...')
init_db() init_db()
@ -221,6 +192,7 @@ if __name__ == "__main__":
parser.add_argument('--host-product') parser.add_argument('--host-product')
parser.add_argument('--host-serial') parser.add_argument('--host-serial')
parser.add_argument('--simulate-virtualbox', action='store_true') parser.add_argument('--simulate-virtualbox', action='store_true')
parser.add_argument('--download-fw-only', action='store_true')
args = parser.parse_args() args = parser.parse_args()
@ -256,9 +228,12 @@ if __name__ == "__main__":
fwpath = vfs_initializer.download_and_extract_fw(fwdir, fwpath = vfs_initializer.download_and_extract_fw(fwdir,
fwuri=args.driver_uri) fwuri=args.driver_uri)
input('The device will be now reset to factory and associated to the ' \ shutil.copy(fwpath, '/usr/share/python-validity/')
'current laptop.\nPress Enter to continue (or Ctrl+C to cancel)...')
vfs_initializer.try_factory_reset() if not args.download_fw_only:
vfs_initializer.sleep() input('The device will be now reset to factory and associated to the ' \
vfs_initializer.pair(fwpath) 'current laptop.\nPress Enter to continue (or Ctrl+C to cancel)...')
vfs_initializer.try_factory_reset()
vfs_initializer.sleep()
vfs_initializer.pair()

View file

@ -85,7 +85,16 @@ class Tls():
def __init__(self, usb): def __init__(self, usb):
self.usb = usb self.usb = usb
self.reset() self.reset()
self.set_hwkey(product_name='VirtualBox', serial_number='0') try:
with open('/sys/class/dmi/id/product_name', 'r') as node:
product_name = node.read().strip()
with open('/sys/class/dmi/id/product_serial', 'r') as node:
product_serial = node.read().strip()
except:
product_name = 'VirtualBox'
product_serial = '0'
self.set_hwkey(product_name=product_name, serial_number=product_serial)
def reset(self): def reset(self):
self.trace_enabled = False self.trace_enabled = False