2023-11-03 19:12:48 +01:00
|
|
|
from time import sleep
|
2021-03-08 17:47:45 +01:00
|
|
|
import traceback
|
|
|
|
from . import ui
|
2023-11-03 19:12:48 +01:00
|
|
|
from fastbootpy import FastbootDevice, FastbootManager
|
2021-03-08 17:47:45 +01:00
|
|
|
|
|
|
|
def handle_exception(e: Exception, message: str):
|
|
|
|
ui.error(message)
|
|
|
|
traceback.print_exc()
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
class Fastboot:
|
|
|
|
def connect(self):
|
2023-11-03 19:12:48 +01:00
|
|
|
ui.info("Waiting for fastboot device")
|
2021-03-08 17:47:45 +01:00
|
|
|
while True:
|
2023-11-03 19:12:48 +01:00
|
|
|
devices = FastbootManager.devices()
|
|
|
|
if len(devices) == 1:
|
|
|
|
self.fb_dev = FastbootDevice.connect(devices[0])
|
|
|
|
ui.info(f"Connected to device {devices[0]}")
|
2021-03-08 17:47:45 +01:00
|
|
|
break
|
2023-11-03 19:12:48 +01:00
|
|
|
elif len(devices) > 1:
|
|
|
|
ui.error("More than one fastboot device is connected!")
|
2021-03-08 17:47:45 +01:00
|
|
|
|
|
|
|
def write_nvme(self, prop: str, data: bytes):
|
2023-11-03 19:12:48 +01:00
|
|
|
cmd = f"getvar:nve:{prop}@".encode('UTF-8')
|
|
|
|
cmd += data
|
2023-11-04 15:57:16 +01:00
|
|
|
ui.debug(f"Sending command: {cmd}")
|
|
|
|
ui.info(f"Writing {prop}")
|
2023-11-03 19:12:48 +01:00
|
|
|
result = self.fb_dev.send(cmd)
|
2023-11-04 15:57:16 +01:00
|
|
|
if not "set nv ok" in result:
|
|
|
|
ui.error(f"Failed to write {prop}: {result}", critical=True)
|
2021-03-08 17:47:45 +01:00
|
|
|
|
|
|
|
def reboot(self):
|
2023-11-03 19:12:48 +01:00
|
|
|
result = self.fb_dev.reboot()
|
|
|
|
ui.info(f"Reboot result: {result}")
|
|
|
|
|
2021-03-08 17:47:45 +01:00
|
|
|
def reboot_bootloader(self):
|
2023-11-03 19:12:48 +01:00
|
|
|
result = self.fb_dev.reboot_bootloader()
|
|
|
|
ui.info(f"Reboot bootloader result: {result}")
|
|
|
|
|