2023-02-06 17:56:36 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import logging
|
|
|
|
import os
|
2023-08-23 21:04:12 +03:00
|
|
|
import re
|
2023-02-06 17:56:36 +03:00
|
|
|
import socket
|
2023-05-03 08:48:49 +03:00
|
|
|
import subprocess
|
|
|
|
import time
|
|
|
|
import typing
|
2023-02-06 17:56:36 +03:00
|
|
|
from abc import ABC, abstractmethod
|
2023-08-22 21:33:10 +03:00
|
|
|
from dataclasses import dataclass, field
|
2023-05-03 08:48:49 +03:00
|
|
|
|
2023-02-06 17:56:36 +03:00
|
|
|
from flipper.app import App
|
2023-09-01 04:23:37 +03:00
|
|
|
from serial.tools.list_ports_common import ListPortInfo
|
2023-02-06 17:56:36 +03:00
|
|
|
|
2023-08-23 21:04:12 +03:00
|
|
|
# When adding an interface, also add it to SWD_TRANSPORT in fbt/ufbt options
|
2023-08-22 21:33:10 +03:00
|
|
|
|
|
|
|
|
2023-02-06 17:56:36 +03:00
|
|
|
class Programmer(ABC):
|
2023-08-23 21:04:12 +03:00
|
|
|
root_logger = logging.getLogger("Programmer")
|
|
|
|
|
2023-02-06 17:56:36 +03:00
|
|
|
@abstractmethod
|
2023-08-22 21:33:10 +03:00
|
|
|
def flash(self, file_path: str, do_verify: bool) -> bool:
|
2023-02-06 17:56:36 +03:00
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def probe(self) -> bool:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def get_name(self) -> str:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def set_serial(self, serial: str):
|
|
|
|
pass
|
|
|
|
|
2023-08-23 21:04:12 +03:00
|
|
|
@classmethod
|
|
|
|
def _spawn_and_await(cls, process_params, show_progress: bool = False):
|
|
|
|
cls.root_logger.debug(f"Launching: {' '.join(process_params)}")
|
|
|
|
|
|
|
|
process = subprocess.Popen(
|
|
|
|
process_params,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
)
|
|
|
|
|
|
|
|
if show_progress:
|
|
|
|
while process.poll() is None:
|
|
|
|
time.sleep(0.25)
|
|
|
|
print(".", end="", flush=True)
|
|
|
|
print()
|
|
|
|
else:
|
|
|
|
process.wait()
|
|
|
|
|
|
|
|
return process
|
|
|
|
|
2023-02-06 17:56:36 +03:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class OpenOCDInterface:
|
|
|
|
name: str
|
2023-08-22 21:33:10 +03:00
|
|
|
config_file: str
|
2023-02-06 17:56:36 +03:00
|
|
|
serial_cmd: str
|
2023-08-22 21:33:10 +03:00
|
|
|
additional_args: typing.Optional[list[str]] = field(default_factory=list)
|
2023-02-06 17:56:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
class OpenOCDProgrammer(Programmer):
|
|
|
|
def __init__(self, interface: OpenOCDInterface):
|
|
|
|
self.interface = interface
|
2023-08-23 21:04:12 +03:00
|
|
|
self.logger = self.root_logger.getChild("OpenOCD")
|
2023-02-06 17:56:36 +03:00
|
|
|
self.serial: typing.Optional[str] = None
|
|
|
|
|
|
|
|
def _add_file(self, params: list[str], file: str):
|
2023-08-22 21:33:10 +03:00
|
|
|
params += ["-f", file]
|
2023-02-06 17:56:36 +03:00
|
|
|
|
|
|
|
def _add_command(self, params: list[str], command: str):
|
2023-08-22 21:33:10 +03:00
|
|
|
params += ["-c", command]
|
2023-02-06 17:56:36 +03:00
|
|
|
|
|
|
|
def _add_serial(self, params: list[str], serial: str):
|
|
|
|
self._add_command(params, f"{self.interface.serial_cmd} {serial}")
|
|
|
|
|
|
|
|
def set_serial(self, serial: str):
|
|
|
|
self.serial = serial
|
|
|
|
|
2023-08-22 21:33:10 +03:00
|
|
|
def flash(self, file_path: str, do_verify: bool) -> bool:
|
2023-02-06 17:56:36 +03:00
|
|
|
if os.altsep:
|
2023-08-22 21:33:10 +03:00
|
|
|
file_path = file_path.replace(os.sep, os.altsep)
|
2023-02-06 17:56:36 +03:00
|
|
|
|
|
|
|
openocd_launch_params = ["openocd"]
|
2023-08-22 21:33:10 +03:00
|
|
|
self._add_file(openocd_launch_params, self.interface.config_file)
|
2023-02-06 17:56:36 +03:00
|
|
|
if self.serial:
|
|
|
|
self._add_serial(openocd_launch_params, self.serial)
|
2023-09-01 04:23:37 +03:00
|
|
|
if self.interface.additional_args:
|
|
|
|
for additional_arg in self.interface.additional_args:
|
|
|
|
self._add_command(openocd_launch_params, additional_arg)
|
2023-02-06 17:56:36 +03:00
|
|
|
self._add_file(openocd_launch_params, "target/stm32wbx.cfg")
|
|
|
|
self._add_command(openocd_launch_params, "init")
|
2023-08-22 21:33:10 +03:00
|
|
|
program_params = [
|
|
|
|
"program",
|
|
|
|
f'"{file_path}"',
|
|
|
|
"verify" if do_verify else "",
|
|
|
|
"reset",
|
|
|
|
"exit",
|
|
|
|
"0x8000000" if file_path.endswith(".bin") else "",
|
|
|
|
]
|
|
|
|
self._add_command(openocd_launch_params, " ".join(program_params))
|
2023-02-06 17:56:36 +03:00
|
|
|
|
|
|
|
# join the list of parameters into a string, but add quote if there are spaces
|
|
|
|
openocd_launch_params_string = " ".join(
|
|
|
|
[f'"{p}"' if " " in p else p for p in openocd_launch_params]
|
|
|
|
)
|
|
|
|
|
|
|
|
self.logger.debug(f"Launching: {openocd_launch_params_string}")
|
|
|
|
|
2023-08-23 21:04:12 +03:00
|
|
|
process = self._spawn_and_await(openocd_launch_params, True)
|
2023-02-06 17:56:36 +03:00
|
|
|
success = process.returncode == 0
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
self.logger.error("OpenOCD failed to flash")
|
|
|
|
if process.stdout:
|
|
|
|
self.logger.error(process.stdout.read().decode("utf-8").strip())
|
|
|
|
|
|
|
|
return success
|
|
|
|
|
|
|
|
def probe(self) -> bool:
|
|
|
|
openocd_launch_params = ["openocd"]
|
2023-08-23 21:04:12 +03:00
|
|
|
self._add_file(openocd_launch_params, self.interface.config_file)
|
2023-02-06 17:56:36 +03:00
|
|
|
if self.serial:
|
|
|
|
self._add_serial(openocd_launch_params, self.serial)
|
2023-09-01 04:23:37 +03:00
|
|
|
if self.interface.additional_args:
|
|
|
|
for additional_arg in self.interface.additional_args:
|
|
|
|
self._add_command(openocd_launch_params, additional_arg)
|
2023-02-06 17:56:36 +03:00
|
|
|
self._add_file(openocd_launch_params, "target/stm32wbx.cfg")
|
|
|
|
self._add_command(openocd_launch_params, "init")
|
|
|
|
self._add_command(openocd_launch_params, "exit")
|
|
|
|
|
2023-08-23 21:04:12 +03:00
|
|
|
process = self._spawn_and_await(openocd_launch_params)
|
|
|
|
success = process.returncode == 0
|
2023-02-06 17:56:36 +03:00
|
|
|
|
2023-08-23 21:04:12 +03:00
|
|
|
output = process.stdout.read().decode("utf-8").strip() if process.stdout else ""
|
|
|
|
self.logger.debug(output)
|
|
|
|
# Find target voltage using regex
|
|
|
|
if match := re.search(r"Target voltage: (\d+\.\d+)", output):
|
|
|
|
voltage = float(match.group(1))
|
|
|
|
if not success:
|
|
|
|
if voltage < 1:
|
|
|
|
self.logger.warning(
|
|
|
|
f"Found {self.get_name()}, but device is not connected"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.logger.warning(
|
|
|
|
f"Device is connected, but {self.get_name()} failed to attach. Is System>Debug enabled?"
|
|
|
|
)
|
|
|
|
|
|
|
|
if "cannot read IDR" in output:
|
|
|
|
self.logger.warning(
|
|
|
|
f"Found {self.get_name()}, but failed to attach. Is device connected and is System>Debug enabled?"
|
|
|
|
)
|
|
|
|
success = False
|
2023-02-06 17:56:36 +03:00
|
|
|
|
2023-08-23 21:04:12 +03:00
|
|
|
return success
|
2023-02-06 17:56:36 +03:00
|
|
|
|
|
|
|
def get_name(self) -> str:
|
|
|
|
return self.interface.name
|
|
|
|
|
|
|
|
|
|
|
|
def blackmagic_find_serial(serial: str):
|
|
|
|
import serial.tools.list_ports as list_ports
|
|
|
|
|
|
|
|
if serial and os.name == "nt":
|
|
|
|
if not serial.startswith("\\\\.\\"):
|
|
|
|
serial = f"\\\\.\\{serial}"
|
|
|
|
|
2023-09-01 04:23:37 +03:00
|
|
|
# idk why, but python thinks that list_ports.grep returns tuple[str, str, str]
|
|
|
|
ports: list[ListPortInfo] = list(list_ports.grep("blackmagic")) # type: ignore
|
|
|
|
|
2023-02-06 17:56:36 +03:00
|
|
|
if len(ports) == 0:
|
|
|
|
return None
|
|
|
|
elif len(ports) > 2:
|
|
|
|
if serial:
|
|
|
|
ports = list(
|
|
|
|
filter(
|
|
|
|
lambda p: p.serial_number == serial
|
|
|
|
or p.name == serial
|
|
|
|
or p.device == serial,
|
|
|
|
ports,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if len(ports) == 0:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if len(ports) > 2:
|
|
|
|
raise Exception("More than one Blackmagic probe found")
|
|
|
|
|
|
|
|
# If you're getting any issues with auto lookup, uncomment this
|
|
|
|
# print("\n".join([f"{p.device} {vars(p)}" for p in ports]))
|
|
|
|
port = sorted(ports, key=lambda p: f"{p.location}_{p.name}")[0]
|
|
|
|
|
|
|
|
if serial:
|
|
|
|
if (
|
|
|
|
serial != port.serial_number
|
|
|
|
and serial != port.name
|
|
|
|
and serial != port.device
|
|
|
|
):
|
|
|
|
return None
|
|
|
|
|
|
|
|
if os.name == "nt":
|
|
|
|
port.device = f"\\\\.\\{port.device}"
|
|
|
|
return port.device
|
|
|
|
|
|
|
|
|
|
|
|
def _resolve_hostname(hostname):
|
|
|
|
try:
|
|
|
|
return socket.gethostbyname(hostname)
|
|
|
|
except socket.gaierror:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def blackmagic_find_networked(serial: str):
|
2023-08-22 21:33:10 +03:00
|
|
|
if not serial or serial == "auto":
|
2023-02-06 17:56:36 +03:00
|
|
|
serial = "blackmagic.local"
|
|
|
|
|
|
|
|
# remove the tcp: prefix if it's there
|
|
|
|
if serial.startswith("tcp:"):
|
|
|
|
serial = serial[4:]
|
|
|
|
|
|
|
|
# remove the port if it's there
|
|
|
|
if ":" in serial:
|
|
|
|
serial = serial.split(":")[0]
|
|
|
|
|
|
|
|
if not (probe := _resolve_hostname(serial)):
|
|
|
|
return None
|
|
|
|
|
|
|
|
return f"tcp:{probe}:2345"
|
|
|
|
|
|
|
|
|
|
|
|
class BlackmagicProgrammer(Programmer):
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
port_resolver, # typing.Callable[typing.Union[str, None], typing.Optional[str]]
|
|
|
|
name: str,
|
|
|
|
):
|
|
|
|
self.port_resolver = port_resolver
|
|
|
|
self.name = name
|
2023-08-23 21:04:12 +03:00
|
|
|
self.logger = self.root_logger.getChild(f"Blackmagic{name}")
|
2023-02-06 17:56:36 +03:00
|
|
|
self.port: typing.Optional[str] = None
|
|
|
|
|
|
|
|
def _add_command(self, params: list[str], command: str):
|
|
|
|
params.append("-ex")
|
|
|
|
params.append(command)
|
|
|
|
|
|
|
|
def _valid_ip(self, address):
|
|
|
|
try:
|
|
|
|
socket.inet_aton(address)
|
|
|
|
return True
|
2023-05-03 08:48:49 +03:00
|
|
|
except Exception:
|
2023-02-06 17:56:36 +03:00
|
|
|
return False
|
|
|
|
|
|
|
|
def set_serial(self, serial: str):
|
|
|
|
if self._valid_ip(serial):
|
|
|
|
self.port = f"{serial}:2345"
|
|
|
|
elif ip := _resolve_hostname(serial):
|
|
|
|
self.port = f"{ip}:2345"
|
|
|
|
else:
|
|
|
|
self.port = serial
|
|
|
|
|
2023-08-23 21:04:12 +03:00
|
|
|
def _get_gdb_core_params(self) -> list[str]:
|
|
|
|
gdb_launch_params = ["arm-none-eabi-gdb"]
|
|
|
|
self._add_command(gdb_launch_params, f"target extended-remote {self.port}")
|
|
|
|
self._add_command(gdb_launch_params, "set pagination off")
|
|
|
|
self._add_command(gdb_launch_params, "set confirm off")
|
|
|
|
self._add_command(gdb_launch_params, "monitor swdp_scan")
|
|
|
|
return gdb_launch_params
|
|
|
|
|
2023-08-22 21:33:10 +03:00
|
|
|
def flash(self, file_path: str, do_verify: bool) -> bool:
|
2023-02-06 17:56:36 +03:00
|
|
|
if not self.port:
|
|
|
|
if not self.probe():
|
|
|
|
return False
|
|
|
|
|
|
|
|
# We can convert .bin to .elf with objcopy:
|
|
|
|
# arm-none-eabi-objcopy -I binary -O elf32-littlearm --change-section-address=.data=0x8000000 -B arm -S app.bin app.elf
|
|
|
|
# But I choose to use the .elf file directly because we are flashing our own firmware and it always has an elf predecessor.
|
2023-08-22 21:33:10 +03:00
|
|
|
|
|
|
|
if file_path.endswith(".bin"):
|
|
|
|
file_path = file_path[:-4] + ".elf"
|
|
|
|
if not os.path.exists(file_path):
|
|
|
|
self.logger.error(
|
|
|
|
f"Sorry, but Blackmagic can't flash .bin file, and {file_path} doesn't exist"
|
|
|
|
)
|
|
|
|
return False
|
2023-02-06 17:56:36 +03:00
|
|
|
|
|
|
|
# arm-none-eabi-gdb build/f7-firmware-D/firmware.bin
|
|
|
|
# -ex 'set pagination off'
|
|
|
|
# -ex 'target extended-remote /dev/cu.usbmodem21201'
|
|
|
|
# -ex 'set confirm off'
|
|
|
|
# -ex 'monitor swdp_scan'
|
|
|
|
# -ex 'attach 1'
|
|
|
|
# -ex 'set mem inaccessible-by-default off'
|
|
|
|
# -ex 'load'
|
|
|
|
# -ex 'compare-sections'
|
|
|
|
# -ex 'quit'
|
|
|
|
|
2023-08-23 21:04:12 +03:00
|
|
|
gdb_launch_params = self._get_gdb_core_params()
|
2023-02-06 17:56:36 +03:00
|
|
|
self._add_command(gdb_launch_params, "attach 1")
|
|
|
|
self._add_command(gdb_launch_params, "set mem inaccessible-by-default off")
|
|
|
|
self._add_command(gdb_launch_params, "load")
|
2023-08-22 21:33:10 +03:00
|
|
|
if do_verify:
|
|
|
|
self._add_command(gdb_launch_params, "compare-sections")
|
2023-02-06 17:56:36 +03:00
|
|
|
self._add_command(gdb_launch_params, "quit")
|
2023-08-23 21:04:12 +03:00
|
|
|
gdb_launch_params.append(file_path)
|
2023-02-06 17:56:36 +03:00
|
|
|
|
2023-08-23 21:04:12 +03:00
|
|
|
process = self._spawn_and_await(gdb_launch_params, True)
|
2023-02-06 17:56:36 +03:00
|
|
|
if not process.stdout:
|
|
|
|
return False
|
|
|
|
|
|
|
|
output = process.stdout.read().decode("utf-8").strip()
|
2023-08-23 21:04:12 +03:00
|
|
|
flashed = (
|
|
|
|
"Loading section .text," in output
|
|
|
|
and "MIS-MATCHED!" not in output
|
|
|
|
and "target image does not match the loaded file" not in output
|
|
|
|
)
|
2023-02-06 17:56:36 +03:00
|
|
|
|
|
|
|
if not flashed:
|
|
|
|
self.logger.error("Blackmagic failed to flash")
|
|
|
|
self.logger.error(output)
|
|
|
|
|
|
|
|
return flashed
|
|
|
|
|
|
|
|
def probe(self) -> bool:
|
|
|
|
if not (port := self.port_resolver(self.port)):
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.port = port
|
2023-08-23 21:04:12 +03:00
|
|
|
|
|
|
|
gdb_launch_params = self._get_gdb_core_params()
|
|
|
|
self._add_command(gdb_launch_params, "quit")
|
|
|
|
|
|
|
|
process = self._spawn_and_await(gdb_launch_params)
|
|
|
|
if not process.stdout or process.returncode != 0:
|
|
|
|
return False
|
|
|
|
|
|
|
|
output = process.stdout.read().decode("utf-8").strip()
|
|
|
|
if "SW-DP scan failed!" in output:
|
|
|
|
self.logger.warning(
|
|
|
|
f"Found {self.get_name()} at {self.port}, but failed to attach. Is device connected and is System>Debug enabled?"
|
|
|
|
)
|
|
|
|
return False
|
2023-02-06 17:56:36 +03:00
|
|
|
return True
|
|
|
|
|
|
|
|
def get_name(self) -> str:
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2023-08-22 21:33:10 +03:00
|
|
|
####################
|
|
|
|
|
|
|
|
local_flash_interfaces: list[Programmer] = [
|
2023-02-06 17:56:36 +03:00
|
|
|
OpenOCDProgrammer(
|
|
|
|
OpenOCDInterface(
|
|
|
|
"cmsis-dap",
|
|
|
|
"interface/cmsis-dap.cfg",
|
|
|
|
"cmsis_dap_serial",
|
|
|
|
["transport select swd"],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
OpenOCDProgrammer(
|
|
|
|
OpenOCDInterface(
|
2023-08-22 21:33:10 +03:00
|
|
|
"stlink",
|
|
|
|
"interface/stlink.cfg",
|
|
|
|
"hla_serial",
|
|
|
|
["transport select hla_swd"],
|
2023-02-06 17:56:36 +03:00
|
|
|
),
|
|
|
|
),
|
|
|
|
BlackmagicProgrammer(blackmagic_find_serial, "blackmagic_usb"),
|
|
|
|
]
|
|
|
|
|
2023-08-22 21:33:10 +03:00
|
|
|
network_flash_interfaces: list[Programmer] = [
|
2023-02-06 17:56:36 +03:00
|
|
|
BlackmagicProgrammer(blackmagic_find_networked, "blackmagic_wifi")
|
|
|
|
]
|
|
|
|
|
2023-08-22 21:33:10 +03:00
|
|
|
all_flash_interfaces = [*local_flash_interfaces, *network_flash_interfaces]
|
|
|
|
|
|
|
|
####################
|
|
|
|
|
2023-02-06 17:56:36 +03:00
|
|
|
|
|
|
|
class Main(App):
|
2023-08-22 21:33:10 +03:00
|
|
|
AUTO_INTERFACE = "auto"
|
|
|
|
|
2023-02-06 17:56:36 +03:00
|
|
|
def init(self):
|
2023-08-23 21:04:12 +03:00
|
|
|
Programmer.root_logger = self.logger
|
|
|
|
|
2023-08-22 21:33:10 +03:00
|
|
|
self.parser.add_argument(
|
|
|
|
"filename",
|
2023-02-06 17:56:36 +03:00
|
|
|
type=str,
|
2023-08-22 21:33:10 +03:00
|
|
|
help="File to flash",
|
|
|
|
)
|
|
|
|
self.parser.add_argument(
|
|
|
|
"--verify",
|
|
|
|
"-v",
|
|
|
|
action="store_true",
|
|
|
|
help="Verify flash after programming",
|
|
|
|
default=False,
|
2023-02-06 17:56:36 +03:00
|
|
|
)
|
2023-08-22 21:33:10 +03:00
|
|
|
self.parser.add_argument(
|
2023-02-06 17:56:36 +03:00
|
|
|
"--interface",
|
2023-08-22 21:33:10 +03:00
|
|
|
choices=(
|
|
|
|
self.AUTO_INTERFACE,
|
|
|
|
*[i.get_name() for i in all_flash_interfaces],
|
|
|
|
),
|
2023-02-06 17:56:36 +03:00
|
|
|
type=str,
|
2023-08-22 21:33:10 +03:00
|
|
|
default=self.AUTO_INTERFACE,
|
2023-02-06 17:56:36 +03:00
|
|
|
help="Interface to use",
|
|
|
|
)
|
2023-08-22 21:33:10 +03:00
|
|
|
self.parser.add_argument(
|
2023-02-06 17:56:36 +03:00
|
|
|
"--serial",
|
|
|
|
type=str,
|
2023-08-22 21:33:10 +03:00
|
|
|
default=self.AUTO_INTERFACE,
|
2023-02-06 17:56:36 +03:00
|
|
|
help="Serial number or port of the programmer",
|
|
|
|
)
|
2023-08-22 21:33:10 +03:00
|
|
|
self.parser.set_defaults(func=self.flash)
|
2023-02-06 17:56:36 +03:00
|
|
|
|
2023-08-22 21:33:10 +03:00
|
|
|
def _search_interface(self, interface_list: list[Programmer]) -> list[Programmer]:
|
2023-02-06 17:56:36 +03:00
|
|
|
found_programmers = []
|
|
|
|
|
2023-08-22 21:33:10 +03:00
|
|
|
for p in interface_list:
|
2023-02-06 17:56:36 +03:00
|
|
|
name = p.get_name()
|
2023-08-22 21:33:10 +03:00
|
|
|
if (serial := self.args.serial) != self.AUTO_INTERFACE:
|
2023-02-06 17:56:36 +03:00
|
|
|
p.set_serial(serial)
|
|
|
|
self.logger.debug(f"Trying {name} with {serial}")
|
|
|
|
else:
|
|
|
|
self.logger.debug(f"Trying {name}")
|
|
|
|
|
|
|
|
if p.probe():
|
|
|
|
self.logger.debug(f"Found {name}")
|
2023-08-22 21:33:10 +03:00
|
|
|
found_programmers.append(p)
|
2023-02-06 17:56:36 +03:00
|
|
|
else:
|
|
|
|
self.logger.debug(f"Failed to probe {name}")
|
|
|
|
|
|
|
|
return found_programmers
|
|
|
|
|
|
|
|
def flash(self):
|
|
|
|
start_time = time.time()
|
2023-08-22 21:33:10 +03:00
|
|
|
file_path = os.path.abspath(self.args.filename)
|
2023-02-06 17:56:36 +03:00
|
|
|
|
2023-08-22 21:33:10 +03:00
|
|
|
if not os.path.exists(file_path):
|
|
|
|
self.logger.error(f"Binary file not found: {file_path}")
|
2023-02-06 17:56:36 +03:00
|
|
|
return 1
|
|
|
|
|
2023-08-22 21:33:10 +03:00
|
|
|
if self.args.interface != self.AUTO_INTERFACE:
|
|
|
|
available_interfaces = list(
|
|
|
|
filter(
|
|
|
|
lambda p: p.get_name() == self.args.interface,
|
|
|
|
all_flash_interfaces,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-02-06 17:56:36 +03:00
|
|
|
else:
|
2023-08-22 21:33:10 +03:00
|
|
|
self.logger.info("Probing for local interfaces...")
|
|
|
|
available_interfaces = self._search_interface(local_flash_interfaces)
|
2023-02-06 17:56:36 +03:00
|
|
|
|
2023-08-22 21:33:10 +03:00
|
|
|
if not available_interfaces:
|
2023-02-06 17:56:36 +03:00
|
|
|
# Probe network blackmagic
|
2023-05-03 08:48:49 +03:00
|
|
|
self.logger.info("Probing for network interfaces...")
|
2023-08-22 21:33:10 +03:00
|
|
|
available_interfaces = self._search_interface(network_flash_interfaces)
|
2023-02-06 17:56:36 +03:00
|
|
|
|
2023-08-22 21:33:10 +03:00
|
|
|
if not available_interfaces:
|
2023-08-23 21:04:12 +03:00
|
|
|
self.logger.error("No availiable interfaces")
|
2023-02-06 17:56:36 +03:00
|
|
|
return 1
|
2023-08-22 21:33:10 +03:00
|
|
|
elif len(available_interfaces) > 1:
|
2023-08-23 21:04:12 +03:00
|
|
|
self.logger.error("Multiple interfaces found:")
|
2023-02-06 17:56:36 +03:00
|
|
|
self.logger.error(
|
2023-08-22 21:33:10 +03:00
|
|
|
f"Please specify '--interface={[i.get_name() for i in available_interfaces]}'"
|
2023-02-06 17:56:36 +03:00
|
|
|
)
|
|
|
|
return 1
|
|
|
|
|
2023-08-22 21:33:10 +03:00
|
|
|
interface = available_interfaces.pop(0)
|
2023-02-06 17:56:36 +03:00
|
|
|
|
2023-08-22 21:33:10 +03:00
|
|
|
if self.args.serial != self.AUTO_INTERFACE:
|
2023-02-06 17:56:36 +03:00
|
|
|
interface.set_serial(self.args.serial)
|
2023-08-23 21:04:12 +03:00
|
|
|
self.logger.info(f"Using {interface.get_name()} with {self.args.serial}")
|
2023-02-06 17:56:36 +03:00
|
|
|
else:
|
2023-08-23 21:04:12 +03:00
|
|
|
self.logger.info(f"Using {interface.get_name()}")
|
|
|
|
self.logger.info(f"Flashing {file_path}")
|
2023-02-06 17:56:36 +03:00
|
|
|
|
2023-08-22 21:33:10 +03:00
|
|
|
if not interface.flash(file_path, self.args.verify):
|
2023-02-06 17:56:36 +03:00
|
|
|
self.logger.error(f"Failed to flash via {interface.get_name()}")
|
|
|
|
return 1
|
|
|
|
|
|
|
|
flash_time = time.time() - start_time
|
|
|
|
self.logger.info(f"Flashed successfully in {flash_time:.2f}s")
|
2023-08-22 21:33:10 +03:00
|
|
|
if file_path.endswith(".bin"):
|
|
|
|
bin_size = os.path.getsize(file_path)
|
|
|
|
self.logger.info(
|
|
|
|
f"Effective speed: {bin_size / flash_time / 1024:.2f} KiB/s"
|
|
|
|
)
|
2023-02-06 17:56:36 +03:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
Main()()
|