add min version requirement and current version to kernel and loader

This commit is contained in:
bunnie 2022-07-08 18:56:05 +08:00
parent 1da8578459
commit 1256fcb411
5 changed files with 50 additions and 68 deletions

7
Cargo.lock generated
View file

@ -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"

View file

@ -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

View file

@ -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"

View file

@ -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<S, T>(
input: &S,
output: &T,
private_key: &pem::Pem,
defile: bool,
minver: &Option<SemVer>,
) -> Result<(), Box<dyn std::error::Error>>
where
S: AsRef<Path>,
@ -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<dyn std::error::Error>> {
.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<dyn std::error::Error>> {
)
.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<dyn std::error::Error>> {
&loader_output,
&loader_pkey,
matches.is_present("defile"),
&minver,
)?;
}
@ -239,6 +201,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
&kernel_output,
&kernel_pkey,
matches.is_present("defile"),
&minver,
)?;
}
Ok(())

View file

@ -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<dyn std::error::Error>;
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()?;