Intelligent probing with warnings for fwflash.py (#3005)
* scripts: fwflash: intelligent probing with warnings * scripts: fwflash: better texting Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
parent
b368660d48
commit
56b5b35236
@ -15,10 +15,9 @@ class App:
|
|||||||
# Application specific initialization
|
# Application specific initialization
|
||||||
self.init()
|
self.init()
|
||||||
|
|
||||||
def __call__(self, args=None, skip_logger_init=False):
|
def __call__(self, args=None):
|
||||||
self.args, self.other_args = self.parser.parse_known_args(args=args)
|
self.args, self.other_args = self.parser.parse_known_args(args=args)
|
||||||
# configure log output
|
# configure log output
|
||||||
# if skip_logger_init:
|
|
||||||
self.log_level = logging.DEBUG if self.args.debug else logging.INFO
|
self.log_level = logging.DEBUG if self.args.debug else logging.INFO
|
||||||
self.logger.setLevel(self.log_level)
|
self.logger.setLevel(self.log_level)
|
||||||
if not self.logger.hasHandlers():
|
if not self.logger.hasHandlers():
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import socket
|
import socket
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
@ -10,11 +11,12 @@ from dataclasses import dataclass, field
|
|||||||
|
|
||||||
from flipper.app import App
|
from flipper.app import App
|
||||||
|
|
||||||
|
# When adding an interface, also add it to SWD_TRANSPORT in fbt/ufbt options
|
||||||
# When adding an interface, also add it to SWD_TRANSPORT in fbt options
|
|
||||||
|
|
||||||
|
|
||||||
class Programmer(ABC):
|
class Programmer(ABC):
|
||||||
|
root_logger = logging.getLogger("Programmer")
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def flash(self, file_path: str, do_verify: bool) -> bool:
|
def flash(self, file_path: str, do_verify: bool) -> bool:
|
||||||
pass
|
pass
|
||||||
@ -31,6 +33,26 @@ class Programmer(ABC):
|
|||||||
def set_serial(self, serial: str):
|
def set_serial(self, serial: str):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@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
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class OpenOCDInterface:
|
class OpenOCDInterface:
|
||||||
@ -43,7 +65,7 @@ class OpenOCDInterface:
|
|||||||
class OpenOCDProgrammer(Programmer):
|
class OpenOCDProgrammer(Programmer):
|
||||||
def __init__(self, interface: OpenOCDInterface):
|
def __init__(self, interface: OpenOCDInterface):
|
||||||
self.interface = interface
|
self.interface = interface
|
||||||
self.logger = logging.getLogger("OpenOCD")
|
self.logger = self.root_logger.getChild("OpenOCD")
|
||||||
self.serial: typing.Optional[str] = None
|
self.serial: typing.Optional[str] = None
|
||||||
|
|
||||||
def _add_file(self, params: list[str], file: str):
|
def _add_file(self, params: list[str], file: str):
|
||||||
@ -87,17 +109,7 @@ class OpenOCDProgrammer(Programmer):
|
|||||||
|
|
||||||
self.logger.debug(f"Launching: {openocd_launch_params_string}")
|
self.logger.debug(f"Launching: {openocd_launch_params_string}")
|
||||||
|
|
||||||
process = subprocess.Popen(
|
process = self._spawn_and_await(openocd_launch_params, True)
|
||||||
openocd_launch_params,
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
stderr=subprocess.STDOUT,
|
|
||||||
)
|
|
||||||
|
|
||||||
while process.poll() is None:
|
|
||||||
time.sleep(0.25)
|
|
||||||
print(".", end="", flush=True)
|
|
||||||
print()
|
|
||||||
|
|
||||||
success = process.returncode == 0
|
success = process.returncode == 0
|
||||||
|
|
||||||
if not success:
|
if not success:
|
||||||
@ -108,35 +120,41 @@ class OpenOCDProgrammer(Programmer):
|
|||||||
return success
|
return success
|
||||||
|
|
||||||
def probe(self) -> bool:
|
def probe(self) -> bool:
|
||||||
i = self.interface
|
|
||||||
|
|
||||||
openocd_launch_params = ["openocd"]
|
openocd_launch_params = ["openocd"]
|
||||||
self._add_file(openocd_launch_params, i.config_file)
|
self._add_file(openocd_launch_params, self.interface.config_file)
|
||||||
if self.serial:
|
if self.serial:
|
||||||
self._add_serial(openocd_launch_params, self.serial)
|
self._add_serial(openocd_launch_params, self.serial)
|
||||||
if i.additional_args:
|
for additional_arg in self.interface.additional_args:
|
||||||
for a in i.additional_args:
|
self._add_command(openocd_launch_params, additional_arg)
|
||||||
self._add_command(openocd_launch_params, a)
|
|
||||||
self._add_file(openocd_launch_params, "target/stm32wbx.cfg")
|
self._add_file(openocd_launch_params, "target/stm32wbx.cfg")
|
||||||
self._add_command(openocd_launch_params, "init")
|
self._add_command(openocd_launch_params, "init")
|
||||||
self._add_command(openocd_launch_params, "exit")
|
self._add_command(openocd_launch_params, "exit")
|
||||||
|
|
||||||
self.logger.debug(f"Launching: {' '.join(openocd_launch_params)}")
|
process = self._spawn_and_await(openocd_launch_params)
|
||||||
|
success = process.returncode == 0
|
||||||
|
|
||||||
process = subprocess.Popen(
|
output = process.stdout.read().decode("utf-8").strip() if process.stdout else ""
|
||||||
openocd_launch_params,
|
self.logger.debug(output)
|
||||||
stderr=subprocess.STDOUT,
|
# Find target voltage using regex
|
||||||
stdout=subprocess.PIPE,
|
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?"
|
||||||
|
)
|
||||||
|
|
||||||
# Wait for OpenOCD to end and get the return code
|
if "cannot read IDR" in output:
|
||||||
process.wait()
|
self.logger.warning(
|
||||||
found = process.returncode == 0
|
f"Found {self.get_name()}, but failed to attach. Is device connected and is System>Debug enabled?"
|
||||||
|
)
|
||||||
|
success = False
|
||||||
|
|
||||||
if process.stdout:
|
return success
|
||||||
self.logger.debug(process.stdout.read().decode("utf-8").strip())
|
|
||||||
|
|
||||||
return found
|
|
||||||
|
|
||||||
def get_name(self) -> str:
|
def get_name(self) -> str:
|
||||||
return self.interface.name
|
return self.interface.name
|
||||||
@ -218,7 +236,7 @@ class BlackmagicProgrammer(Programmer):
|
|||||||
):
|
):
|
||||||
self.port_resolver = port_resolver
|
self.port_resolver = port_resolver
|
||||||
self.name = name
|
self.name = name
|
||||||
self.logger = logging.getLogger("BlackmagicUSB")
|
self.logger = self.root_logger.getChild(f"Blackmagic{name}")
|
||||||
self.port: typing.Optional[str] = None
|
self.port: typing.Optional[str] = None
|
||||||
|
|
||||||
def _add_command(self, params: list[str], command: str):
|
def _add_command(self, params: list[str], command: str):
|
||||||
@ -240,6 +258,14 @@ class BlackmagicProgrammer(Programmer):
|
|||||||
else:
|
else:
|
||||||
self.port = serial
|
self.port = serial
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
def flash(self, file_path: str, do_verify: bool) -> bool:
|
def flash(self, file_path: str, do_verify: bool) -> bool:
|
||||||
if not self.port:
|
if not self.port:
|
||||||
if not self.probe():
|
if not self.probe():
|
||||||
@ -268,43 +294,25 @@ class BlackmagicProgrammer(Programmer):
|
|||||||
# -ex 'compare-sections'
|
# -ex 'compare-sections'
|
||||||
# -ex 'quit'
|
# -ex 'quit'
|
||||||
|
|
||||||
gdb_launch_params = ["arm-none-eabi-gdb", file_path]
|
gdb_launch_params = self._get_gdb_core_params()
|
||||||
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")
|
|
||||||
self._add_command(gdb_launch_params, "attach 1")
|
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, "set mem inaccessible-by-default off")
|
||||||
self._add_command(gdb_launch_params, "load")
|
self._add_command(gdb_launch_params, "load")
|
||||||
if do_verify:
|
if do_verify:
|
||||||
self._add_command(gdb_launch_params, "compare-sections")
|
self._add_command(gdb_launch_params, "compare-sections")
|
||||||
self._add_command(gdb_launch_params, "quit")
|
self._add_command(gdb_launch_params, "quit")
|
||||||
|
gdb_launch_params.append(file_path)
|
||||||
|
|
||||||
self.logger.debug(f"Launching: {' '.join(gdb_launch_params)}")
|
process = self._spawn_and_await(gdb_launch_params, True)
|
||||||
|
|
||||||
process = subprocess.Popen(
|
|
||||||
gdb_launch_params,
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
stderr=subprocess.STDOUT,
|
|
||||||
)
|
|
||||||
|
|
||||||
while process.poll() is None:
|
|
||||||
time.sleep(0.5)
|
|
||||||
print(".", end="", flush=True)
|
|
||||||
print()
|
|
||||||
|
|
||||||
if not process.stdout:
|
if not process.stdout:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
output = process.stdout.read().decode("utf-8").strip()
|
output = process.stdout.read().decode("utf-8").strip()
|
||||||
flashed = "Loading section .text," in output
|
flashed = (
|
||||||
|
"Loading section .text," in output
|
||||||
# Check flash verification
|
and "MIS-MATCHED!" not in output
|
||||||
if "MIS-MATCHED!" in output:
|
and "target image does not match the loaded file" not in output
|
||||||
flashed = False
|
)
|
||||||
|
|
||||||
if "target image does not match the loaded file" in output:
|
|
||||||
flashed = False
|
|
||||||
|
|
||||||
if not flashed:
|
if not flashed:
|
||||||
self.logger.error("Blackmagic failed to flash")
|
self.logger.error("Blackmagic failed to flash")
|
||||||
@ -317,6 +325,20 @@ class BlackmagicProgrammer(Programmer):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
self.port = port
|
self.port = port
|
||||||
|
|
||||||
|
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
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def get_name(self) -> str:
|
def get_name(self) -> str:
|
||||||
@ -358,6 +380,8 @@ class Main(App):
|
|||||||
AUTO_INTERFACE = "auto"
|
AUTO_INTERFACE = "auto"
|
||||||
|
|
||||||
def init(self):
|
def init(self):
|
||||||
|
Programmer.root_logger = self.logger
|
||||||
|
|
||||||
self.parser.add_argument(
|
self.parser.add_argument(
|
||||||
"filename",
|
"filename",
|
||||||
type=str,
|
type=str,
|
||||||
@ -433,10 +457,10 @@ class Main(App):
|
|||||||
available_interfaces = self._search_interface(network_flash_interfaces)
|
available_interfaces = self._search_interface(network_flash_interfaces)
|
||||||
|
|
||||||
if not available_interfaces:
|
if not available_interfaces:
|
||||||
self.logger.error("No interface found")
|
self.logger.error("No availiable interfaces")
|
||||||
return 1
|
return 1
|
||||||
elif len(available_interfaces) > 1:
|
elif len(available_interfaces) > 1:
|
||||||
self.logger.error("Multiple interfaces found: ")
|
self.logger.error("Multiple interfaces found:")
|
||||||
self.logger.error(
|
self.logger.error(
|
||||||
f"Please specify '--interface={[i.get_name() for i in available_interfaces]}'"
|
f"Please specify '--interface={[i.get_name() for i in available_interfaces]}'"
|
||||||
)
|
)
|
||||||
@ -446,11 +470,10 @@ class Main(App):
|
|||||||
|
|
||||||
if self.args.serial != self.AUTO_INTERFACE:
|
if self.args.serial != self.AUTO_INTERFACE:
|
||||||
interface.set_serial(self.args.serial)
|
interface.set_serial(self.args.serial)
|
||||||
self.logger.info(
|
self.logger.info(f"Using {interface.get_name()} with {self.args.serial}")
|
||||||
f"Flashing {file_path} via {interface.get_name()} with {self.args.serial}"
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
self.logger.info(f"Flashing {file_path} via {interface.get_name()}")
|
self.logger.info(f"Using {interface.get_name()}")
|
||||||
|
self.logger.info(f"Flashing {file_path}")
|
||||||
|
|
||||||
if not interface.flash(file_path, self.args.verify):
|
if not interface.flash(file_path, self.args.verify):
|
||||||
self.logger.error(f"Failed to flash via {interface.get_name()}")
|
self.logger.error(f"Failed to flash via {interface.get_name()}")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user