diff --git a/tools/Cargo.toml b/tools/Cargo.toml index c4b9b31dd..13a51b2bf 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -29,6 +29,8 @@ aes-gcm-siv = "0.11.1" bao1x-api = { path = "../libs/bao1x-api" } hex-literal = "1.0.0" byteorder = "1.2" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" [[bin]] name = "copy-object" diff --git a/tools/src/sign_image.rs b/tools/src/sign_image.rs index 36146448b..bac9256ef 100644 --- a/tools/src/sign_image.rs +++ b/tools/src/sign_image.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; +use std::fs; use std::io::{Read, Write}; use std::path::Path; @@ -186,6 +188,8 @@ pub fn sign_image( Ok(dest_file) } Version::Bao1xV1 => { + let anti_rollback = read_anti_rollback("./signing/anti-rollback.hjson")?; + let pkinfo = PrivateKeyInfo::from_der(&private_key.contents).map_err(|e| format!("{}", e))?; // First 2 bytes of the `private_key` are a record specifier and length field. Check they are // correct. @@ -203,6 +207,9 @@ pub fn sign_image( .map_err(|e| format!("{}", e))?; let pubkey = sk.public_key(); */ + let anti_rollback = anti_rollback + .get(function_code.unwrap_or("unspecified")) + .expect("anti-rollback code not specified"); let function_code = match function_code { Some("boot0") => FunctionCode::Boot0, @@ -218,7 +225,7 @@ pub fn sign_image( header.sealed_data.version = version as u32; header.sealed_data.signed_len = (source.len() + SIGBLOCK_LEN - UNSIGNED_LEN) as u32; header.sealed_data.function_code = function_code as u32; - header.sealed_data.reserved = 0; + header.sealed_data.anti_rollback = *anti_rollback; header.sealed_data.min_semver = minver_bytes; header.sealed_data.semver = semver; @@ -407,3 +414,33 @@ pub fn bin_to_uf2(bytes: &Vec, family_id: u32, app_start_addr: u32) -> Resul } Ok(outp) } + +/// Strips // comments from HJSON and returns valid JSON +fn hjson_to_json(hjson: &str) -> String { + hjson + .lines() + .map(|line| { + // Find the position of // comment marker + if let Some(pos) = line.find("//") { &line[..pos] } else { line } + }) + .collect::>() + .join("\n") +} + +/// Reads HJSON config file and returns a HashMap for lookups +fn read_anti_rollback>(path: P) -> Result, Box> { + let content = fs::read_to_string(path)?; + let json_content = hjson_to_json(&content); + + // Parse as HashMap with string values first + let string_map: HashMap = serde_json::from_str(&json_content)?; + + // Convert string values to u32 + let mut result = HashMap::new(); + for (key, value) in string_map { + let version: u32 = value.parse()?; + result.insert(key, version); + } + + Ok(result) +}