mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
update tools to generate a uf2 image
This commit is contained in:
parent
d0bd60d93c
commit
d9347e46fb
3 changed files with 111 additions and 1 deletions
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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<dyn std::error::Error>> {
|
|||
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<dyn std::error::Error>> {
|
|||
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(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -287,3 +287,88 @@ where
|
|||
dest_file.write_all(&result)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn convert_to_uf2<S, T>(
|
||||
input: &S,
|
||||
output: &T,
|
||||
function_code: Option<&str>,
|
||||
app_id: Option<u8>,
|
||||
) -> Result<(), Box<dyn std::error::Error>>
|
||||
where
|
||||
S: AsRef<Path>,
|
||||
T: AsRef<Path>,
|
||||
{
|
||||
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<u8>, family_id: u32, app_start_addr: u32) -> Result<Vec<u8>, std::io::Error> {
|
||||
let datapadding = [0u8; 512 - 256 - 32 - 4];
|
||||
let nblocks: u32 = ((bytes.len() + 255) / 256) as u32;
|
||||
let mut outp: Vec<u8> = 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::<LittleEndian>(UF2_MAGIC_START0)?;
|
||||
outp.write_u32::<LittleEndian>(UF2_MAGIC_START1)?;
|
||||
outp.write_u32::<LittleEndian>(flags)?;
|
||||
outp.write_u32::<LittleEndian>(ptr + app_start_addr)?;
|
||||
outp.write_u32::<LittleEndian>(256)?;
|
||||
outp.write_u32::<LittleEndian>(blockno)?;
|
||||
outp.write_u32::<LittleEndian>(nblocks)?;
|
||||
outp.write_u32::<LittleEndian>(family_id)?;
|
||||
|
||||
// data
|
||||
outp.write(&chunk)?;
|
||||
outp.write(&datapadding)?;
|
||||
|
||||
// footer
|
||||
outp.write_u32::<LittleEndian>(UF2_MAGIC_END)?;
|
||||
}
|
||||
Ok(outp)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue