Skip to content

Commit

Permalink
xtask: Include expected measurement for hubris images in build archive.
Browse files Browse the repository at this point in the history
This implementation is currently limited to stm32h7 images for the SP.
  • Loading branch information
flihp committed Aug 16, 2023
1 parent 4176179 commit 614898a
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/gemini-bu/app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ target = "thumbv7em-none-eabihf"
board = "gemini-bu-1"
chip = "../../chips/stm32h7"
stacksize = 896
fwid = true

[kernel]
name = "gemini-bu"
Expand Down
1 change: 1 addition & 0 deletions app/gimlet/base.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ target = "thumbv7em-none-eabihf"
chip = "../../chips/stm32h7"
memory = "memory-large.toml"
stacksize = 896
fwid = true

[kernel]
name = "gimlet"
Expand Down
1 change: 1 addition & 0 deletions app/gimletlet/app-meanwell.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ target = "thumbv7em-none-eabihf"
board = "gimletlet-2"
chip = "../../chips/stm32h7"
stacksize = 896
fwid = true

[kernel]
name = "gimletlet"
Expand Down
1 change: 1 addition & 0 deletions app/gimletlet/app-mgmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ target = "thumbv7em-none-eabihf"
board = "gimletlet-1"
chip = "../../chips/stm32h7"
stacksize = 1024
fwid = true

[kernel]
name = "gimletlet"
Expand Down
1 change: 1 addition & 0 deletions app/gimletlet/app-sidecar-emulator.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ board = "gimletlet-2"
chip = "../../chips/stm32h7"
memory = "memory-large.toml"
stacksize = 896
fwid = true

[kernel]
name = "gimletlet"
Expand Down
1 change: 1 addition & 0 deletions app/gimletlet/app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ memory = "memory-large.toml"
stacksize = 896
epoch = 0
version = 0
fwid = true

[kernel]
name = "gimletlet"
Expand Down
1 change: 1 addition & 0 deletions app/psc/base.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ target = "thumbv7em-none-eabihf"
chip = "../../chips/stm32h7"
memory = "memory-large.toml"
stacksize = 896
fwid = true

[kernel]
name = "psc"
Expand Down
1 change: 1 addition & 0 deletions app/sidecar/base.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ target = "thumbv7em-none-eabihf"
chip = "../../chips/stm32h7"
stacksize = 896
memory = "memory-large.toml"
fwid = true

[kernel]
name = "sidecar"
Expand Down
1 change: 1 addition & 0 deletions build/xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dunce = { workspace = true }
filetime = { workspace = true }
fnv = { workspace = true }
goblin = { workspace = true }
hex = "0.4"
hubtools = { workspace = true }
indexmap = { workspace = true }
multimap = { workspace = true }
Expand Down
4 changes: 4 additions & 0 deletions build/xtask/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ struct RawConfig {
epoch: u32,
#[serde(default)]
version: u32,
#[serde(default)]
fwid: bool,
memory: Option<String>,
#[serde(default)]
image_names: Vec<String>,
Expand All @@ -52,6 +54,7 @@ pub struct Config {
pub chip: String,
pub epoch: u32,
pub version: u32,
pub fwid: bool,
pub image_names: Vec<String>,
pub external_images: Vec<String>,
pub signing: Option<RoTMfgSettings>,
Expand Down Expand Up @@ -177,6 +180,7 @@ impl Config {
chip: toml.chip,
epoch: toml.epoch,
version: toml.version,
fwid: toml.fwid,
signing: toml.signing,
stacksize: toml.stacksize,
kernel: toml.kernel,
Expand Down
54 changes: 54 additions & 0 deletions build/xtask/src/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};
use std::ffi::OsStr;
use std::fmt::Write as _;
use std::fs::{self, File};
use std::hash::{Hash, Hasher};
Expand All @@ -16,6 +17,7 @@ use atty::Stream;
use indexmap::IndexMap;
use multimap::MultiMap;
use path_slash::{PathBufExt, PathExt};
use sha3::{Digest, Sha3_256};
use zerocopy::AsBytes;

use crate::{
Expand Down Expand Up @@ -535,6 +537,10 @@ pub fn package(
archive.overwrite()?;
}

if cfg.toml.fwid {
write_fwid(&cfg, &image_name, &flash, &archive_name)?;
}

// Unzip the signed + caboose'd images into our build directory
let archive = hubtools::RawHubrisArchive::load(&archive_name)
.context("loading archive with hubtools")?;
Expand All @@ -549,6 +555,54 @@ pub fn package(
Ok(allocated)
}

// generate file with hash of expected flash contents
fn write_fwid(
cfg: &PackageConfig,
image_name: &String,
flash: &Range<u32>,
archive_name: &PathBuf,
) -> Result<()> {
let mut archive = hubtools::RawHubrisArchive::load(archive_name)
.context("loading archive with hubtools")?;

let bin = archive
.extract_file("img/final.bin")
.context("extracting final.bin after signing & caboosing")?;

let chip_name = Path::new(&cfg.toml.chip);

// determine FWID calculation method from chip (directory) name
let pad = match chip_name.file_name().and_then(OsStr::to_str) {
Some("stm32h7") => {
// all unprogrammed flash is read as 0xff
Some(vec![
0xff_u8;
flash.end as usize - flash.start as usize - bin.len()
])
}
Some(_) => None,
None => bail!("Failed to get file name of {}", chip_name.display()),
};

let mut sha = Sha3_256::new();
sha.update(&bin);

if let Some(pad) = pad {
sha.update(&pad);
}

let digest = sha.finalize();

let mut file = File::create(&cfg.img_file("final.fwid", image_name))?;
let fwid = hex::encode(&digest);
writeln!(file, "{}", fwid)?;

archive.add_file("img/final.fwid", fwid.as_bytes())?;
archive.overwrite()?;

Ok(())
}

fn write_gdb_script(cfg: &PackageConfig, image_name: &str) -> Result<()> {
// Humility doesn't know about images right now. The gdb symbol file
// paths all assume a flat layout with everything in dist. For now,
Expand Down

0 comments on commit 614898a

Please sign in to comment.