add jump past signature block

Make the first word in the signature block a jump past the block
this is for the cramium target, where the best memory efficiency
happens if we align the data to the bottom of the memory. This
means that the signature block ends up "in the way" of the boot
process, because the reset vector can only be configured to something
on a 16k-page boundary (e.g., we'd waste 15k of memory without this).

This will require some reworking of the existing code base for
checking signatures down the line.
This commit is contained in:
bunnie 2024-04-26 18:13:06 +08:00
parent 8746fc5a76
commit 9d2218488b
2 changed files with 46 additions and 6 deletions

View file

@ -74,6 +74,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Arg::with_name("defile")
.help("patch the resulting image, to create a test file to catch signature failure"),
)
.arg(
Arg::with_name("with-jump")
.long("with-jump")
.takes_value(false)
.help("Insert a jump instruction in the signature block"),
)
.get_matches();
let minver =
@ -96,7 +102,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Err("invalid loader private key type")?;
}
println!("Signing loader");
sign_file(&loader_image, &loader_output, &loader_pkey, matches.is_present("defile"), &minver, false)?;
sign_file(
&loader_image,
&loader_output,
&loader_pkey,
matches.is_present("defile"),
&minver,
false,
matches.is_present("with-jump"),
)?;
}
if let Some(kernel_output) = matches.value_of("kernel-output") {
@ -109,7 +123,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Err("invalid kernel private key type")?;
}
println!("Signing kernel");
sign_file(&kernel_image, &kernel_output, &kernel_pkey, matches.is_present("defile"), &minver, true)?;
sign_file(
&kernel_image,
&kernel_output,
&kernel_pkey,
matches.is_present("defile"),
&minver,
true,
matches.is_present("with-jump"),
)?;
}
Ok(())
}

View file

@ -9,6 +9,7 @@ use sha2::{Digest, Sha512};
const LOADER_VERSION: u32 = 1;
const LOADER_PREHASH_VERSION: u32 = 2;
const RV_SKIP_I: u32 = 0x0000106f; // jal x0, 4096
use xous_semver::SemVer;
@ -26,6 +27,7 @@ pub fn sign_image(
defile: bool,
minver: &Option<SemVer>,
semver: Option<[u8; 16]>,
with_jump: bool,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
let mut source = source.to_owned();
let mut dest_file = vec![];
@ -66,6 +68,13 @@ pub fn sign_image(
Ed25519KeyPair::from_pkcs8_maybe_unchecked(&private_key.contents).map_err(|e| format!("{}", e))?;
let signature = signing_key.sign(&source);
let extra_pad = if with_jump {
dest_file.write_all(&RV_SKIP_I.to_le_bytes())?;
4
} else {
0
};
dest_file.write_all(&LOADER_VERSION.to_le_bytes())?;
dest_file.write_all(&(source.len() as u32).to_le_bytes())?;
@ -75,7 +84,7 @@ pub fn sign_image(
// Pad the first sector to 4096 bytes.
let mut v = vec![];
v.resize(4096 - 4 - 4 - signature_u8.len(), 0);
v.resize(4096 - 4 - 4 - signature_u8.len() - extra_pad, 0);
dest_file.write_all(&v)?;
// Fill the remainder of the source data
@ -98,6 +107,7 @@ pub fn sign_image_prehash(
defile: bool,
minver: &Option<SemVer>,
semver: Option<[u8; 16]>,
with_jump: bool,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
let mut source = source.to_owned();
let mut dest_file = vec![];
@ -145,6 +155,13 @@ pub fn sign_image_prehash(
let signing_key = SigningKey::from_bytes(&secbytes);
let signature = signing_key.sign_digest(h);
let extra_pad = if with_jump {
dest_file.write_all(&RV_SKIP_I.to_le_bytes())?;
4
} else {
0
};
dest_file.write_all(&LOADER_PREHASH_VERSION.to_le_bytes())?;
dest_file.write_all(&(source.len() as u32).to_le_bytes())?;
@ -154,7 +171,7 @@ pub fn sign_image_prehash(
// Pad the first sector to 4096 bytes.
let mut v = vec![];
v.resize(4096 - 4 - 4 - signature_u8.len(), 0);
v.resize(4096 - 4 - 4 - signature_u8.len() - extra_pad, 0);
dest_file.write_all(&v)?;
// Fill the remainder of the source data
@ -178,6 +195,7 @@ pub fn sign_file<S, T>(
defile: bool,
minver: &Option<SemVer>,
use_prehash: bool,
with_jump: bool,
) -> Result<(), Box<dyn std::error::Error>>
where
S: AsRef<Path>,
@ -189,9 +207,9 @@ where
source_file.read_to_end(&mut source)?;
let result = if use_prehash {
sign_image_prehash(&source, private_key, defile, minver, None)?
sign_image_prehash(&source, private_key, defile, minver, None, with_jump)?
} else {
sign_image(&source, private_key, defile, minver, None)?
sign_image(&source, private_key, defile, minver, None, with_jump)?
};
dest_file.write_all(&result)?;
Ok(())