fix(core/build): improve robustness of xtask build
What changed, and why it matters
This commit is a routine internal cleanup of the Trezor firmware build tooling. It makes the build helper code more robust by consistently reporting errors instead of silently using defaults, and it centralizes how the build system loads hardware model configuration. There is no change to the firmware that runs on the device, no fix for a vulnerability, and no indication of a security issue.
No security action required. Treat as a normal build-system maintainability improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors the Rust xtask build scripts in core/embed/xtask and the upymod build.rs. Key changes: (1) MpyBuilder::new and Model::dependency now return Result so configuration-loading failures propagate as errors rather than being unwrapped or silently defaulted; (2) BoardConfig peripheral parsing now returns an explicit error for malformed TOML instead of defaulting to empty features; (3) several call sites switch from config::ModelConfig::load(args.model.model_id()) to a new args.model.config() helper. These are defensive code-quality improvements in the build-time tooling. No runtime firmware logic, cryptographic code, or device behavior is modified.
Changed components
core/embed/upymod/build.rscore/embed/xtask/src/args.rscore/embed/xtask/src/cargo.rscore/embed/xtask/src/config.rscore/embed/xtask/src/feature_resolver.rscore/embed/xtask/src/flash.rscore/embed/xtask/src/helpers.rscore/embed/xtask/src/model.rsInspect captured patch +31 / −31
diff --git a/core/embed/upymod/build.rs b/core/embed/upymod/build.rs
index 484b67bf..48071270 100644
--- a/core/embed/upymod/build.rs
+++ b/core/embed/upymod/build.rs
@@ -292,7 +292,7 @@ fn main() -> Result<()> {
let scm_revision_xor2 = define_scm_revision(lib)?;
// Build content of genhdr folder
- let mpy_builder = MpyBuilder::new(lib, scm_revision_xor2);
+ let mpy_builder = MpyBuilder::new(lib, scm_revision_xor2)?;
let qstr_preprocessed = mpy_builder.build_genhdr()?;
if cfg!(feature = "frozen") && !xbuild::is_rust_analyzer() {
@@ -409,7 +409,7 @@ struct MpyBuilder<'a> {
}
impl<'a> MpyBuilder<'a> {
- fn new(lib: &'a CLibrary, scm_revision_xor2: u8) -> Self {
+ fn new(lib: &'a CLibrary, scm_revision_xor2: u8) -> Result<Self> {
let crate_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let mpy_dir = crate_dir.join("../../vendor/micropython");
let py_src_dir = crate_dir.join("../../src");
@@ -417,7 +417,7 @@ impl<'a> MpyBuilder<'a> {
let genhdr_dir = out_dir.join("genhdr");
let current_model = current_model_id()?;
- Self {
+ Ok(Self {
lib,
crate_dir,
mpy_dir,
@@ -426,7 +426,7 @@ impl<'a> MpyBuilder<'a> {
py_src_dir,
scm_revision_xor2,
current_model,
- }
+ })
}
fn build_genhdr(&self) -> Result<PathBuf> {
diff --git a/core/embed/xtask/src/args.rs b/core/embed/xtask/src/args.rs
index 0c7602d6..a65b1a05 100644
--- a/core/embed/xtask/src/args.rs
+++ b/core/embed/xtask/src/args.rs
@@ -2,8 +2,6 @@ use anyhow::{Result, anyhow};
use clap::{Args, Parser, Subcommand, ValueEnum};
use std::process;
-use crate::config;
-
pub use crate::model::Model;
pub struct ResolvedBuild {
@@ -82,20 +80,18 @@ impl Component {
/// Returns the next component in the dependency chain,
/// (e.g. Firmware -> Kernel -> Secmon)
- pub fn dependency(self, model: Model) -> Option<Component> {
+ pub fn dependency(self, model: Model) -> Result<Option<Component>> {
match self {
- Component::Firmware => Some(Component::Kernel),
+ Component::Firmware => Ok(Some(Component::Kernel)),
Component::Kernel => {
- let has_secmon = config::ModelConfig::load(model.model_id())
- .map(|c| c.secmon)
- .unwrap_or(false);
- if has_secmon {
+ let has_secmon = model.config()?.secmon;
+ Ok(if has_secmon {
Some(Component::Secmon)
} else {
None
- }
+ })
}
- _ => None,
+ _ => Ok(None),
}
}
diff --git a/core/embed/xtask/src/cargo.rs b/core/embed/xtask/src/cargo.rs
index b6a1d90c..82d1425d 100644
--- a/core/embed/xtask/src/cargo.rs
+++ b/core/embed/xtask/src/cargo.rs
@@ -4,7 +4,7 @@ use std::process;
use crate::{
args::{BuildArgs, Component, TestArgs},
- artifacts, config, helpers, memusage, postbuild, prebuild,
+ artifacts, helpers, memusage, postbuild, prebuild,
};
pub fn build(args: BuildArgs) -> Result<()> {
@@ -91,7 +91,7 @@ pub fn fmt() -> Result<()> {
fn build_impl(args: BuildArgs, is_dependency: bool) -> Result<()> {
if !args.emulator {
// Recursively build dependencies (Firmware -> Kernel -> Secmon)
- if let Some(dependency) = args.component.dependency(args.model) {
+ if let Some(dependency) = args.component.dependency(args.model)? {
build_impl(
BuildArgs {
component: dependency,
@@ -116,7 +116,7 @@ fn build_impl(args: BuildArgs, is_dependency: bool) -> Result<()> {
if !args.emulator {
let use_dev_keys = args.bootloader_devel || !args.production;
- let model_config = config::ModelConfig::load(args.model.model_id())?;
+ let model_config = args.model.config()?;
// For hardware targets, we need to convert the ELF file into a raw
// binary before signing it.
diff --git a/core/embed/xtask/src/config.rs b/core/embed/xtask/src/config.rs
index 7a8582cb..33bf214f 100644
--- a/core/embed/xtask/src/config.rs
+++ b/core/embed/xtask/src/config.rs
@@ -103,15 +103,14 @@ impl BoardConfig {
if key == "header" {
continue;
}
- let features = val
+ let periph_table = val
.as_table()
- .map(|t| {
- t.values()
- .filter_map(|v| v.as_str())
- .map(|s| s.to_string())
- .collect()
- })
- .unwrap_or_default();
+ .ok_or_else(|| anyhow!("invalid peripheral '{key}': expected table"))?;
+ let features = periph_table
+ .values()
+ .filter_map(|v| v.as_str())
+ .map(|s| s.to_string())
+ .collect();
peripherals.push(Peripheral {
name: key.clone(),
features,
diff --git a/core/embed/xtask/src/feature_resolver.rs b/core/embed/xtask/src/feature_resolver.rs
index de2100ae..e92af9fa 100644
--- a/core/embed/xtask/src/feature_resolver.rs
+++ b/core/embed/xtask/src/feature_resolver.rs
@@ -140,7 +140,7 @@ pub fn resolve_features(args: &BuildArgs) -> Result<ResolvedBuild> {
}
// Board and model-intrinsic features from TOML config
- let model_config = config::ModelConfig::load(args.model.model_id())?;
+ let model_config = args.model.config()?;
let board_id = if args.emulator {
model_config
.emulator_board
diff --git a/core/embed/xtask/src/flash.rs b/core/embed/xtask/src/flash.rs
index ecd49279..b31dbde1 100644
--- a/core/embed/xtask/src/flash.rs
+++ b/core/embed/xtask/src/flash.rs
@@ -6,7 +6,7 @@ use std::{
use crate::{
args::{FlashArgs, FlashEraseArgs, FlashSection},
- config, helpers,
+ helpers,
};
/// Flashes the specified component to the device using OpenOCD.
@@ -35,7 +35,7 @@ pub fn flash(args: FlashArgs) -> Result<()> {
);
let flash_instruction = build_flash_write_instruction(&binary, address);
- let model_config = config::ModelConfig::load(args.model.model_id())?;
+ let model_config = args.model.config()?;
let status = process::Command::new("openocd")
.args(["-f", "interface/stlink.cfg"])
@@ -58,7 +58,7 @@ 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 = config::ModelConfig::load(args.model.model_id())?;
+ let model_config = args.model.config()?;
let status = process::Command::new("openocd")
.args(["-f", "interface/stlink.cfg"])
diff --git a/core/embed/xtask/src/helpers.rs b/core/embed/xtask/src/helpers.rs
index 65d96055..4fe8ab70 100644
--- a/core/embed/xtask/src/helpers.rs
+++ b/core/embed/xtask/src/helpers.rs
@@ -6,7 +6,6 @@ use std::{
};
use crate::args::{BuildArgs, Component, Model};
-use crate::config;
/// Returns the path to the built ELF file for the given build arguments.
pub fn elf_path(args: &BuildArgs) -> Result<PathBuf> {
@@ -18,7 +17,7 @@ pub fn elf_path(args: &BuildArgs) -> Result<PathBuf> {
pub fn profile_dir(args: &BuildArgs) -> Result<PathBuf> {
let mut path = build_dir()?;
if !args.emulator {
- let model_config = config::ModelConfig::load(args.model.model_id())?;
+ let model_config = args.model.config()?;
path = path.join(model_config.target_triple()?);
}
diff --git a/core/embed/xtask/src/model.rs b/core/embed/xtask/src/model.rs
index 68b168ea..9bcf262b 100644
--- a/core/embed/xtask/src/model.rs
+++ b/core/embed/xtask/src/model.rs
@@ -1,6 +1,7 @@
use anyhow::{Context, Result};
use clap::ValueEnum;
+use crate::config::ModelConfig;
use crate::helpers;
#[derive(ValueEnum, Debug, Clone, Copy, PartialEq, Eq)]
@@ -42,6 +43,11 @@ impl Model {
}
}
+ /// Loads this model's configuration from `models/<id>/model.toml`.
+ pub fn config(self) -> Result<ModelConfig> {
+ ModelConfig::load(self.model_id())
+ }
+
/// Returns the path to the model-specific memory.ld file
pub fn model_memory_ld(self) -> Result<std::path::PathBuf> {
let mem_ld = helpers::workspace_dir()?
Why this scored 13/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.