diff --git a/tools/Cargo.toml b/tools/Cargo.toml index 752db07cc..a4a53ad99 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -28,6 +28,7 @@ rand = "0.8.5" aes-gcm-siv = "0.11.1" bao1x-api = { path = "../libs/bao1x-api" } hex-literal = "1.0.0" +byteorder = "1.2" [[bin]] name = "copy-object" diff --git a/tools/src/bin/sign-image.rs b/tools/src/bin/sign-image.rs index 7f744cd00..6181b9feb 100644 --- a/tools/src/bin/sign-image.rs +++ b/tools/src/bin/sign-image.rs @@ -1,7 +1,7 @@ use std::io::{Error, ErrorKind}; use clap::{App, Arg, crate_version}; -use tools::sign_image::{load_pem, sign_file}; +use tools::sign_image::{convert_to_uf2, load_pem, sign_file}; const DEVKEY_PATH: &str = "devkey/dev.key"; @@ -139,6 +139,17 @@ fn main() -> Result<(), Box> { sig_length, matches.value_of("function-code"), )?; + + if matches.is_present("bao1x") { + if loader_output.ends_with(".img") { + let loader_uf2 = format!("{}uf2", &loader_output[..loader_output.len() - 3]); + // generate a uf2 file + convert_to_uf2(&loader_output, &loader_uf2, matches.value_of("function-code"), None)?; + println!("Created UF2 at {}", loader_uf2); + } else { + Err("Can't generate UF2 file because the output file is not specified with a .img suffix")?; + } + } } if let Some(kernel_output) = matches.value_of("kernel-output") { @@ -167,6 +178,19 @@ fn main() -> Result<(), Box> { sig_length, Some(matches.value_of("function-code").unwrap_or("kernel")), )?; + + if matches.is_present("bao1x") { + if kernel_output.ends_with(".img") { + let kernel_uf2 = format!("{}uf2", &kernel_output[..kernel_output.len() - 3]); + // generate a uf2 file + convert_to_uf2(&kernel_output, &kernel_uf2, matches.value_of("function-code"), None)?; + println!("Created UF2 at {}", kernel_uf2); + } else { + Err( + "Can't generate UF2 file because the kernel output file is not specified with a .img suffix", + )?; + } + } } Ok(()) } diff --git a/tools/src/sign_image.rs b/tools/src/sign_image.rs index ebce020f6..98ce800c9 100644 --- a/tools/src/sign_image.rs +++ b/tools/src/sign_image.rs @@ -287,3 +287,88 @@ where dest_file.write_all(&result)?; Ok(()) } + +pub fn convert_to_uf2( + input: &S, + output: &T, + function_code: Option<&str>, + app_id: Option, +) -> Result<(), Box> +where + S: AsRef, + T: AsRef, +{ + let mut source = vec![]; + 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 app_start_addr = match function_code { + Some("boot0") => bao1x_api::BOOT0_START, + Some("boot1") => bao1x_api::BOOT1_START, + Some("loader") => bao1x_api::LOADER_START, + Some("baremetal") => bao1x_api::BAREMETAL_START, + Some("kernel") => bao1x_api::KERNEL_START, + Some("app") => { + bao1x_api::APP_START_UF2 + | ((app_id.expect("app region specified without ID") as usize) & 0xF) << 24 + } + _ => return Err(String::from("UF2 Image Requires a function code").into()), + }; + + match bin_to_uf2(&source, bao1x_api::BAOCHIP_1X_UF2_FAMILY, app_start_addr as u32) { + Ok(u2f_blob) => { + dest_file.write_all(&u2f_blob)?; + Ok(()) + } + Err(e) => Err(e.into()), + } +} + +use byteorder::{LittleEndian, WriteBytesExt}; + +const UF2_MAGIC_START0: u32 = 0x0A324655; // "UF2\n" +const UF2_MAGIC_START1: u32 = 0x9E5D5157; // Randomly selected +const UF2_MAGIC_END: u32 = 0x0AB16F30; // Ditto +// Vendored in from https://github.com/sajattack/uf2conv-rs/blob/master/lib/src/lib.rs +// The code is MIT-licensed, and authored by Paul Sajna +pub fn bin_to_uf2(bytes: &Vec, family_id: u32, app_start_addr: u32) -> Result, std::io::Error> { + let datapadding = [0u8; 512 - 256 - 32 - 4]; + let nblocks: u32 = ((bytes.len() + 255) / 256) as u32; + let mut outp: Vec = Vec::new(); + for blockno in 0..nblocks { + let ptr = 256 * blockno; + let chunk = match bytes.get(ptr as usize..ptr as usize + 256) { + Some(bytes) => bytes.to_vec(), + None => { + let mut chunk = bytes[ptr as usize..bytes.len()].to_vec(); + while chunk.len() < 256 { + chunk.push(0); + } + chunk + } + }; + let mut flags: u32 = 0; + if family_id != 0 { + flags |= 0x2000 + } + + // header + outp.write_u32::(UF2_MAGIC_START0)?; + outp.write_u32::(UF2_MAGIC_START1)?; + outp.write_u32::(flags)?; + outp.write_u32::(ptr + app_start_addr)?; + outp.write_u32::(256)?; + outp.write_u32::(blockno)?; + outp.write_u32::(nblocks)?; + outp.write_u32::(family_id)?; + + // data + outp.write(&chunk)?; + outp.write(&datapadding)?; + + // footer + outp.write_u32::(UF2_MAGIC_END)?; + } + Ok(outp) +}