refactor(core/build): extract run_openocd_command function
What changed, and why it matters
This commit is a simple code cleanup in the Trezor firmware build tooling. It extracts repeated OpenOCD command construction into a single helper function, with no functional changes to what commands are run or how they behave.
No security action needed; this is a routine refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The refactor moves duplicated process::Command::new("openocd") blocks from flash(), flash_erase(), and reset() into a new private run_openocd(model: Model, instructions: &str) function. The generated command-line arguments, error handling, and external behavior remain identical. No new dependencies, no new inputs, and no changes to argument parsing or privilege boundaries.
Changed components
core/embed/xtask/src/flash.rsInspect captured patch +11 / −30
diff --git a/core/embed/xtask/src/flash.rs b/core/embed/xtask/src/flash.rs
index b20effb9..4ef510ea 100644
--- a/core/embed/xtask/src/flash.rs
+++ b/core/embed/xtask/src/flash.rs
@@ -5,7 +5,7 @@ use std::{
};
use crate::{
- args::{FlashArgs, FlashEraseArgs, FlashSection, ResetArgs},
+ args::{FlashArgs, FlashEraseArgs, FlashSection, Model, ResetArgs},
helpers,
};
@@ -35,20 +35,8 @@ pub fn flash(args: FlashArgs) -> Result<()> {
);
let flash_instruction = build_flash_write_instruction(&binary, address);
- let model_config = args.model.config()?;
- let status = process::Command::new("openocd")
- .args(["-f", "interface/stlink.cfg"])
- .args(["-c", "transport select hla_swd"])
- .args(["-f", model_config.openocd_target()?])
- .arg("-c")
- .arg(flash_instruction)
- .status()
- .context("Failed to spawn `openocd`")?;
-
- ensure!(status.success(), "`openocd` failed with status: {status}");
-
- Ok(())
+ run_openocd(args.model, &flash_instruction)
}
/// Erase specified flash section using OpenOCD. The section boundaries are determined
@@ -58,34 +46,27 @@ pub fn flash_erase(args: FlashEraseArgs) -> Result<()> {
let content = fs::read_to_string(&mem_ld)
.with_context(|| format!("Failed to read `{}`", mem_ld.display()))?;
let instr = build_flash_erase_instruction(&content, args.section)?;
- let model_config = args.model.config()?;
-
- let status = process::Command::new("openocd")
- .args(["-f", "interface/stlink.cfg"])
- .args(["-c", "transport select hla_swd"])
- .args(["-f", model_config.openocd_target()?])
- .arg("-c")
- .arg(instr)
- .status()
- .context("Failed to spawn `openocd`")?;
- ensure!(status.success(), "`openocd` failed with status: {status}");
-
- Ok(())
+ run_openocd(args.model, &instr)
}
/// Resets the connected device using OpenOCD.
pub fn reset(args: ResetArgs) -> Result<()> {
- let model_config = args.model.config()?;
-
println!("Resetting `{:?}`", args.model);
+ run_openocd(args.model, "init; reset; exit")
+}
+
+/// Runs OpenOCD instructions against the connected device for the given model.
+fn run_openocd(model: Model, instructions: &str) -> Result<()> {
+ let model_config = model.config()?;
+
let status = process::Command::new("openocd")
.args(["-f", "interface/stlink.cfg"])
.args(["-c", "transport select hla_swd"])
.args(["-f", model_config.openocd_target()?])
.arg("-c")
- .arg("init; reset; exit")
+ .arg(instructions)
.status()
.context("Failed to spawn `openocd`")?;
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.