Added functionality for running a command before flashing

This commit is contained in:
2025-09-19 17:50:52 +02:00
parent 1b9f1ea4c1
commit dd49c30ac9
4 changed files with 45 additions and 2 deletions

View File

@@ -1,3 +1,4 @@
use std::ffi::OsString;
use std::io::{Cursor, Error, Write};
use std::time::{Duration, SystemTime};
@@ -201,12 +202,35 @@ fn write_chip_data(args: &ArgWrite, target: &mut PhysicalTarget) -> Result<(), E
fn write_chip(args: ArgWrite, target: &mut PhysicalTarget) -> Result<(), Error> {
loop {
if let Some(cmd) = args.cmd.as_ref() {
let mut args = shlex::bytes::split(cmd.as_encoded_bytes())
.ok_or(Error::other("Error parsing commandline arguments"))?
.into_iter()
.map(|x| unsafe { OsString::from_encoded_bytes_unchecked(x) });
let Some(name) = args.next() else {
Err(Error::other("No command provided"))?
};
let out = std::process::Command::new(name)
.args(args)
.output()?;
if !out.status.success() {
Err(Error::other(format!("Command failed with exit code {}:\n\n{}",
out.status.code().unwrap_or(1),
String::from_utf8_lossy(&out.stderr))))?
}
println!("Command `{}` successful.", cmd.to_string_lossy());
}
write_chip_data(&args, target)?;
if !args.monitor { break }
let cause = Monitor::new()
.add_hook(Key::Ctrl('f'), "Re-flash target")
.add_hook(Key::Ctrl('f'), if args.cmd.is_none() { "Re-flash target" } else { "Re-run command and flash target" })
.run(target.port())?;
if cause != Key::Ctrl('f') { break }
@@ -241,7 +265,13 @@ fn main() {
}
None
}).unwrap();
});
let Some(port) = port else {
println!("Could not find any compatible serial port. Please make sure that the target is connected.");
println!("If the issue persists, please specify the port manually with the `--port ...` argument.");
std::process::exit(1);
};
let mut target = PhysicalTarget::init(port, if args.slow { 57600 } else { 312500 }).unwrap();
@@ -254,6 +284,7 @@ fn main() {
if let Err(err) = res {
println!("An error occured during the operation: {}", err);
std::process::exit(1);
}
let end = SystemTime::now();