Merge pull request #817 from sbellem/xtask-git-args

Add --git-describe and --git-rev CLI args
This commit is contained in:
bunnie 2026-02-13 03:03:48 +08:00 committed by GitHub
commit 7c98a86899
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 124 additions and 34 deletions

View file

@ -57,6 +57,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.takes_value(false)
.help("When specified, creates a swap image"),
)
.arg(
Arg::with_name("git-rev")
.long("git-rev")
.takes_value(true)
.required(false)
.help("Explicit git commit hash for swap nonce (e.g., '0d934e1...'). If not specified, uses git rev-parse HEAD."),
)
.get_matches();
let mut process_names = ProcessNames::new();
@ -117,13 +124,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let private_key = pem::parse(DEV_KEY_PEM)?;
let git_rev = matches.value_of("git-rev");
if matches.is_present("swap") {
let mut swap_buffer = SwapWriter::new();
args.write(&mut swap_buffer)?;
// Create the swap target image and encrypt swap_buffer to it
let mut swap = Cursor::new(Vec::new());
swap_buffer.encrypt_to(&mut swap, &private_key, Some(anti_rollback as usize))?;
swap_buffer.encrypt_to(&mut swap, &private_key, Some(anti_rollback as usize), git_rev)?;
// generate a uf2 file
let swap_uf2 = "swap.uf2";

View file

@ -215,6 +215,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.value_name("swap signing key")
.default_value(DEVKEY_PATH),
)
.arg(
Arg::with_name("git-rev")
.long("git-rev")
.takes_value(true)
.required(false)
.help("Explicit git commit hash for swap nonce (e.g., '0d934e1...'). If not specified, uses git rev-parse HEAD.")
.value_name("commit hash"),
)
.get_matches();
let mut ram_config = RamConfig {
@ -497,6 +505,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Err("invalid kernel private key type")?;
}
let git_rev = matches.value_of("git-rev");
if let Some(mut sargs) = swap_args {
let mut swap_buffer = SwapWriter::new();
// Transfer our unencrypted data inside sargs to swap_buffer
@ -508,7 +518,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
{
let sf = File::create(swap_filename)
.unwrap_or_else(|_| panic!("Couldn't create output file {}", swap_filename));
swap_buffer.encrypt_to(sf, &swap_pkey, None).expect("Couldn't flush swap buffer to disk");
swap_buffer
.encrypt_to(sf, &swap_pkey, None, git_rev)
.expect("Couldn't flush swap buffer to disk");
} // drop sf, so it closes
println!("Swap arguments: {}", sargs);
@ -525,7 +537,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut sf =
File::create(sf_dbg).unwrap_or_else(|_| panic!("Couldn't create debug file {}", sf_dbg));
// serialize the unencrypted header into the debug image
let header = xous_tools::swap_writer::SwapHeader::new(buf_len);
let header = xous_tools::swap_writer::SwapHeader::new(buf_len, git_rev);
sf.write(&header.serialize()?)?;
// directly write our unencrypted data from sargs into file sf. It's an exact copy of
// what ended up in swap_buffer.

View file

@ -115,6 +115,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.help("When specified, fills the public key block with random, fake data. Used for testing third-party boot flow policies")
.required(false)
)
.arg(
Arg::with_name("git-describe")
.long("git-describe")
.takes_value(true)
.help("Explicit git describe version to embed (e.g., 'v0.10.0-19-g0d934e1'). If not specified, uses git describe.")
.required(false)
)
.get_matches();
let minver =
@ -132,6 +139,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
None
};
let semver: Option<[u8; 16]> = if let Some(git_describe_str) = matches.value_of("git-describe") {
Some(
SemVer::from_str(git_describe_str)
.map_err(|_| Error::new(ErrorKind::InvalidInput, "git-describe format incorrect"))?
.into(),
)
} else {
None
};
let sig_length = usize::from_str_radix(matches.value_of("sig-length").unwrap_or("4096"), 10)
.expect("sig-length should be a decimal number");
// Sign the loader, if an output file was specified
@ -159,6 +176,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
&loader_pkey,
matches.is_present("defile"),
&minver,
semver,
version,
matches.is_present("with-jump"),
sig_length,
@ -202,6 +220,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
&kernel_pkey,
matches.is_present("defile"),
&minver,
semver,
version,
matches.is_present("with-jump"),
sig_length,

View file

@ -327,6 +327,7 @@ pub fn sign_file<S, T>(
private_key: &pem::Pem,
defile: bool,
minver: &Option<SemVer>,
semver: Option<[u8; 16]>,
version: Version,
with_jump: bool,
sector_length: usize,
@ -348,7 +349,7 @@ where
private_key,
defile,
minver,
None,
semver,
with_jump,
sector_length,
version,

View file

@ -12,28 +12,32 @@ use crate::sign_image::{Version, sign_image};
const SWAP_VERSION: u32 = 0x01_01_0000;
pub fn git_rev() -> u64 {
// Execute the git command
let output =
Command::new("git").args(&["rev-parse", "HEAD"]).output().expect("Failed to execute command");
// Check if the command was successful
if output.status.success() {
// Convert the output bytes to a string
let hash = std::str::from_utf8(&output.stdout).expect("Failed to convert output to string");
// Print the commit hash
let partial_hash =
u64::from_str_radix(&hash.trim_end_matches('\n')[hash.trim_end_matches('\n').len() - 16..], 16)
.expect("couldn't convert git hash");
println!("Current commit hash: {} Extracted nonce: {:x}", hash, partial_hash);
partial_hash
pub fn git_rev(override_rev: Option<&str>) -> u64 {
let hash = if let Some(rev) = override_rev {
rev.to_string()
} else {
// Print an error message if the command failed
let error_message =
std::str::from_utf8(&output.stderr).expect("Failed to convert error message to string");
panic!("Failed to get commit hash: {}", error_message);
}
// Execute the git command
let output =
Command::new("git").args(&["rev-parse", "HEAD"]).output().expect("Failed to execute command");
// Check if the command was successful
if output.status.success() {
// Convert the output bytes to a string
std::str::from_utf8(&output.stdout).expect("Failed to convert output to string").to_string()
} else {
// Print an error message if the command failed
let error_message =
std::str::from_utf8(&output.stderr).expect("Failed to convert error message to string");
panic!("Failed to get commit hash: {}", error_message);
}
};
// Print the commit hash
let partial_hash =
u64::from_str_radix(&hash.trim_end_matches('\n')[hash.trim_end_matches('\n').len() - 16..], 16)
.expect("couldn't convert git hash");
println!("Current commit hash: {} Extracted nonce: {:x}", hash, partial_hash);
partial_hash
}
pub struct SwapWriter {
@ -47,10 +51,10 @@ pub struct SwapHeader {
mac_offset: usize,
}
impl SwapHeader {
pub fn new(swap_len: usize) -> Self {
pub fn new(swap_len: usize, git_rev_override: Option<&str>) -> Self {
SwapHeader {
aad: "swap".as_bytes().to_vec(),
partial_nonce: git_rev(),
partial_nonce: git_rev(git_rev_override),
mac_offset: ((swap_len + 0xFFF) / 0x1000) * 0x1000,
}
}
@ -88,11 +92,12 @@ impl SwapWriter {
mut f: T,
private_key: &pem::Pem,
anti_rollback_manual: Option<usize>,
git_rev_override: Option<&str>,
) -> Result<usize>
where
T: Write + Seek,
{
let header = SwapHeader::new(self.buffer.get_ref().len());
let header = SwapHeader::new(self.buffer.get_ref().len(), git_rev_override);
let mut macs = Vec::<u8>::new();
let unsigned_header = header.serialize()?;

View file

@ -182,6 +182,8 @@ pub(crate) struct Builder {
sigblock_size: usize,
board: String,
detached_app_features: Vec<String>,
git_describe: Option<String>,
git_rev: Option<String>,
}
impl Builder {
@ -215,9 +217,21 @@ impl Builder {
sigblock_size: 4096,
board: String::new(),
detached_app_features: Vec::new(),
git_describe: None,
git_rev: None,
}
}
pub fn set_git_describe(&mut self, git_describe: String) -> &mut Builder {
self.git_describe = Some(git_describe);
self
}
pub fn set_git_rev(&mut self, git_rev: String) -> &mut Builder {
self.git_rev = Some(git_rev);
self
}
/// Sets up the signature block size
pub fn set_sigblock_size<'a>(&'a mut self, size: usize) -> &'a mut Builder {
self.sigblock_size = size;
@ -1003,6 +1017,10 @@ impl Builder {
}
_ => return Err(String::from("Can't determine bootloader region").into()),
};
let git_describe_args: Vec<&str> = match &self.git_describe {
Some(gd) => vec!["--git-describe", gd],
None => vec![],
};
Command::new(cargo())
.current_dir(project_root())
.args([
@ -1027,6 +1045,7 @@ impl Builder {
"--function-code",
function_code,
])
.args(&git_describe_args)
.status()?;
return Ok(());
}
@ -1115,6 +1134,10 @@ impl Builder {
return Err("cargo build failed".into());
}
let git_describe_args: Vec<&str> = match &self.git_describe {
Some(gd) => vec!["--git-describe", gd],
None => vec![],
};
let status = if self.utra_target.contains("bao1x") {
Command::new(cargo())
.current_dir(project_root())
@ -1140,6 +1163,7 @@ impl Builder {
"--function-code",
"loader",
])
.args(&git_describe_args)
.status()?
} else {
Command::new(cargo())
@ -1160,6 +1184,7 @@ impl Builder {
"--min-xous-ver",
&self.min_ver,
])
.args(&git_describe_args)
.status()?
};
if !status.success() {
@ -1195,6 +1220,7 @@ impl Builder {
"kernel",
// "--defile",
])
.args(&git_describe_args)
.status()?
} else {
Command::new(cargo())
@ -1216,6 +1242,7 @@ impl Builder {
&self.min_ver,
// "--defile",
])
.args(&git_describe_args)
.status()?
};
if !status.success() {
@ -1320,6 +1347,11 @@ impl Builder {
}
}
if let Some(ref git_rev) = self.git_rev {
args.push("--git-rev");
args.push(git_rev);
}
let status = Command::new(cargo()).current_dir(project_root()).args(&args).status()?;
if !status.success() {
@ -1363,6 +1395,10 @@ impl Builder {
let mut app_img_path = output_file.parent().unwrap().to_owned();
app_img_path.push("apps.img");
let git_describe_args: Vec<&str> = match &self.git_describe {
Some(gd) => vec!["--git-describe", gd],
None => vec![],
};
Command::new(cargo())
.current_dir(project_root())
.args([
@ -1388,6 +1424,7 @@ impl Builder {
"app",
// "--defile",
])
.args(&git_describe_args)
.status()?;
Ok(project_root().join(output_file))

View file

@ -58,11 +58,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut builder = Builder::new();
// encodes a timestamp into the build, unless '--no-timestamp' is passed
let do_version = env::args().filter(|x| x == "--no-timestamp").count() == 0;
let manual_version = get_flag("--gitrev")?;
if manual_version.is_empty() {
let git_describe = get_flag("--git-describe")?;
let git_rev = get_flag("--git-rev")?;
if git_describe.is_empty() {
generate_version(do_version, None);
} else {
generate_version(do_version, Some(manual_version[0].to_owned()));
generate_version(do_version, Some(git_describe[0].to_owned()));
builder.set_git_describe(git_describe[0].to_owned());
}
if !git_rev.is_empty() {
builder.set_git_rev(git_rev[0].to_owned());
}
if do_version {
builder.add_feature("timestamp");
@ -932,7 +937,8 @@ fn print_help() {
[--debug-loader]
[--offline]
[--change-target]
[--gitrev revision]
[--git-describe version]
[--git-rev commit]
[cratespecs] is a list of 0 or more items of the following syntax:
[name] crate 'name' to be built from local source
@ -959,8 +965,9 @@ be merged in with explicit app/service treatment with the following flags:
[--swap offset:size] Specify a region for swap memory. The behavior of this depends on the target.
[--change-target] Used to clean the cached target/*/*/build/SVD_PATH when changing build targets.
This will also force a full rebuild every time the flag is specified.
[--gitrev revision] Force a git revision and do not execute `git describe --long` as part of the build process. For build systems that lack git state.
Note: there is no sanity checking on the passed revision. If it's specified incorrectly, subtle, weird things could happen.
[--git-describe version] Force a git describe version string (e.g., 'v0.10.0-19-g0d934e1') instead of running `git describe --long`. For build systems that lack git state.
Note: there is no sanity checking on the passed version. If it's specified incorrectly, subtle, weird things could happen.
[--git-rev commit] Force a git commit hash (e.g., '0d934e1...') for swap image nonce. Required with --git-describe for reproducible builds.
- An 'app' must be enumerated in apps/manifest.json.
A pre-processor configures the launch menu based on the list of specified apps.