From 1256fcb4110945c628d8cd81e9c8e4c21e71fbd4 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 8 Jul 2022 18:56:05 +0800 Subject: [PATCH] add min version requirement and current version to kernel and loader --- Cargo.lock | 7 +++ RELEASE-v0.9.md | 3 ++ tools/Cargo.toml | 1 + tools/src/bin/sign-image.rs | 99 ++++++++++++------------------------- xtask/src/main.rs | 8 +++ 5 files changed, 50 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9c861761e..5c9cc55ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3608,6 +3608,7 @@ dependencies = [ "ring", "svd2utra", "xmas-elf", + "xous-semver", ] [[package]] @@ -4359,6 +4360,12 @@ dependencies = [ "xous-ipc 0.9.7", ] +[[package]] +name = "xous-semver" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ecd1a30e0deeb6dd362cb68bec34104d1d91ae7eb02d76beb237d880a44dc77" + [[package]] name = "xous-tts-backend" version = "0.1.3" diff --git a/RELEASE-v0.9.md b/RELEASE-v0.9.md index 069482d5c..30f367716 100644 --- a/RELEASE-v0.9.md +++ b/RELEASE-v0.9.md @@ -197,6 +197,9 @@ perform the Xous firmware upgrade. This requires running manual update commands, - BIP39 wordlist handling - Display of BIP39 words from a `[u8]` - Input of BIP39 words into a `[u8]`, with validation and dynamic lookup/autocomplete of words +- Improved kernel revisioning + - A readable semantic version tag is appended to loader and kernel images + - In addition to the current tag, a tag for "minimum xous version" to read a PDDB backup generated by the current Xous image is included. ## Roadmap to 1.0 diff --git a/tools/Cargo.toml b/tools/Cargo.toml index 31d6efee7..ba5148d17 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -19,6 +19,7 @@ log = "0.4.14" pem = "0.8.3" svd2utra = {path = "../svd2utra"} xmas-elf = "0.7.0" +xous-semver = "0.1.1" [[bin]] name = "copy-object" diff --git a/tools/src/bin/sign-image.rs b/tools/src/bin/sign-image.rs index 1e294c114..b5eb71572 100644 --- a/tools/src/bin/sign-image.rs +++ b/tools/src/bin/sign-image.rs @@ -1,5 +1,5 @@ use clap::{crate_version, App, Arg}; -use std::io::{Read, Write}; +use std::io::{Read, Write, ErrorKind, Error}; use std::path::Path; use ring::signature::Ed25519KeyPair; @@ -7,78 +7,14 @@ use ring::signature::Ed25519KeyPair; const DEVKEY_PATH: &str = "devkey/dev.key"; const LOADER_VERSION: u32 = 1; -use std::process::Command; -use std::convert::{From, Into, TryInto}; +use xous_semver::SemVer; -struct SemVer { - maj: u16, - min: u16, - rev: u16, - extra: u16, - commit: u32, -} -impl SemVer { - pub fn new() -> Self { - let output = if cfg!(target_os = "windows") { - Command::new("cmd") - .args(["/C", "git describe --tags"]) - .output() - .expect("failed to execute process") - } else { - Command::new("sh") - .arg("-c") - .arg("git describe --tags") - .output() - .expect("failed to execute process") - }; - let gitver = output.stdout; - let semver = String::from_utf8_lossy(&gitver); - let ver: Vec<&str> = semver.strip_prefix('v') - .expect("semver does not start with 'v'!") - .split(['.', '-']).collect(); - SemVer { - maj: u16::from_str_radix(ver[0], 10).expect("error parsing semver"), - min: u16::from_str_radix(ver[1], 10).expect("error parsing semver"), - rev: u16::from_str_radix(ver[2], 10).expect("error parsing semver"), - extra: if ver.len() == 5 { - u16::from_str_radix(ver[3], 10).expect("error parsing semver") - } else {0}, // special case when the tag is totally clean - commit: u32::from_str_radix( - ver[ver.len() - 1] - .trim_end() - .strip_prefix('g') - .expect("gitrev malformed"), 16 - ).expect("error parsing semver"), - } - } -} -impl From::<[u8; 12]> for SemVer { - fn from(bytes: [u8; 12]) -> SemVer { - SemVer { - maj: u16::from_le_bytes(bytes[0..2].try_into().unwrap()), - min: u16::from_le_bytes(bytes[2..4].try_into().unwrap()), - rev: u16::from_le_bytes(bytes[4..6].try_into().unwrap()), - extra: u16::from_le_bytes(bytes[6..8].try_into().unwrap()), - commit: u32::from_le_bytes(bytes[8..12].try_into().unwrap()), - } - } -} -impl Into::<[u8; 12]> for SemVer { - fn into(self) -> [u8; 12] { - let mut ser = [0u8; 12]; - ser[0..2].copy_from_slice(&self.maj.to_le_bytes()); - ser[2..4].copy_from_slice(&self.min.to_le_bytes()); - ser[4..6].copy_from_slice(&self.rev.to_le_bytes()); - ser[6..8].copy_from_slice(&self.extra.to_le_bytes()); - ser[8..12].copy_from_slice(&self.commit.to_le_bytes()); - ser - } -} fn image_sign( input: &S, output: &T, private_key: &pem::Pem, defile: bool, + minver: &Option, ) -> Result<(), Box> where S: AsRef, @@ -88,7 +24,14 @@ where let mut source_file = std::fs::File::open(input)?; let mut dest_file = std::fs::File::create(output)?; source_file.read_to_end(&mut source)?; - let semver: [u8; 12] = SemVer::new().into(); + + // Append version information to the binary. Appending it here means it is part + // of the signed bundle. + let minver_bytes = if let Some(mv) = minver {mv.into()} else {[0u8; 16]}; + let semver: [u8; 16] = SemVer::from_git() + .map_err(|_| Error::new(ErrorKind::Other, "error parsing current Git rev"))? + .into(); + source.append(&mut minver_bytes.to_vec()); source.append(&mut semver.to_vec()); for &b in LOADER_VERSION.to_le_bytes().iter() { source.push(b); @@ -190,6 +133,15 @@ fn main() -> Result<(), Box> { .value_name("kernel output image") .help("kernel output image"), ) + .arg( + Arg::with_name("min-xous-ver") + .long("min-xous-ver") + .help("Minimum Xous version for cross-compatibility") + .value_name("min xous ver") + .takes_value(true) + .default_value("v0.9.8-790") + .required(true), + ) .arg( Arg::with_name("defile").help( "patch the resulting image, to create a test file to catch signature failure", @@ -197,6 +149,15 @@ fn main() -> Result<(), Box> { ) .get_matches(); + let minver = if let Some(minver_str) = matches.value_of("min-xous-ver") { + Some( + SemVer::from_str(minver_str) + .map_err(|_| Error::new(ErrorKind::InvalidInput, "Minimum version semver format incorrect"))? + ) + } else { + None + }; + // Sign the loader, if an output file was specified if let Some(loader_output) = matches.value_of("loader-output") { let loader_key = matches @@ -217,6 +178,7 @@ fn main() -> Result<(), Box> { &loader_output, &loader_pkey, matches.is_present("defile"), + &minver, )?; } @@ -239,6 +201,7 @@ fn main() -> Result<(), Box> { &kernel_output, &kernel_pkey, matches.is_present("defile"), + &minver, )?; } Ok(()) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 970a80ccc..533436a9b 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -10,6 +10,10 @@ use std::{ process::Command, }; +// This is the minimum Xous version required to read a PDDB backup generated +// by the current kernel revision. +const MIN_XOUS_VERSION: &str = "v0.9.8-791"; + type DynError = Box; const PROGRAM_TARGET: &str = "riscv32imac-unknown-xous-elf"; @@ -947,6 +951,8 @@ fn build_hw_image( loaderkey_file.as_str(), "--loader-output", loader_bin.to_str().unwrap(), + "--min-xous-ver", + MIN_XOUS_VERSION, ]) .status()?; if !status.success() { @@ -984,6 +990,8 @@ fn build_hw_image( kernelkey_file.as_str(), "--kernel-output", xous_img_path.to_str().unwrap(), + "--min-xous-ver", + MIN_XOUS_VERSION, // "--defile", ]) .status()?;