diff --git a/tools/src/bin/sign-image.rs b/tools/src/bin/sign-image.rs index a873d7adf..cb874e516 100644 --- a/tools/src/bin/sign-image.rs +++ b/tools/src/bin/sign-image.rs @@ -74,6 +74,12 @@ fn main() -> Result<(), Box> { 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> { 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> { 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(()) } diff --git a/tools/src/sign_image.rs b/tools/src/sign_image.rs index a3394adf5..ac26a00ae 100644 --- a/tools/src/sign_image.rs +++ b/tools/src/sign_image.rs @@ -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: Option<[u8; 16]>, + with_jump: bool, ) -> Result, Box> { 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: Option<[u8; 16]>, + with_jump: bool, ) -> Result, Box> { 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( defile: bool, minver: &Option, use_prehash: bool, + with_jump: bool, ) -> Result<(), Box> where S: AsRef, @@ -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(())