mirror of
https://github.com/solokeys/solo2.git
synced 2026-06-20 13:16:13 -07:00
171 lines
6.0 KiB
Rust
171 lines
6.0 KiB
Rust
use std::process::Command;
|
||
use std::str;
|
||
use std::{env, error, fs::File, io::Write, path::Path};
|
||
|
||
/// Waiting on cargo fix!
|
||
// #[derive(serde::Deserialize)]
|
||
struct Config {
|
||
parameters: Parameters,
|
||
}
|
||
|
||
// #[derive(serde::Deserialize)]
|
||
struct Parameters {
|
||
filesystem_boundary: u32,
|
||
}
|
||
|
||
macro_rules! add_build_variable {
|
||
($file:expr, $name:literal, u8) => {
|
||
let value = env!($name);
|
||
let value: u8 = str::parse(value).expect("Version components must be able to fit in a u8.");
|
||
writeln!($file, "pub const {}: u8 = {};", $name, value)
|
||
.expect("Could not write build_constants.rs file");
|
||
};
|
||
|
||
($file:expr, $name:literal, u16) => {
|
||
let value = env!($name);
|
||
let value: u16 =
|
||
str::parse(value).expect("Version components must be able to fit in a u16.");
|
||
writeln!($file, "pub const {}: u16 = {};", $name, value)
|
||
.expect("Could not write build_constants.rs file");
|
||
};
|
||
|
||
($file:expr, $name:literal, $value:expr, u32) => {
|
||
writeln!($file, "pub const {}: u32 = {};", $name, $value)
|
||
.expect("Could not write build_constants.rs file");
|
||
};
|
||
|
||
($file:expr, $name:literal, $value:expr, usize) => {
|
||
writeln!($file, "pub const {}: usize= {};", $name, $value)
|
||
.expect("Could not write build_constants.rs file");
|
||
};
|
||
|
||
($file:expr, $name:literal, $value:expr) => {
|
||
writeln!($file, "pub const {}: &str = \"{}\";", $name, $value)
|
||
.expect("Could not write build_constants.rs file");
|
||
};
|
||
}
|
||
|
||
fn write_memory_x(linker_script_file: &mut File, parameters: &Parameters) {
|
||
writeln!(
|
||
linker_script_file,
|
||
"
|
||
/* DO NOT EDIT THIS FILE */
|
||
/* This file was generated by build.rs */
|
||
|
||
MEMORY
|
||
{{
|
||
FLASH : ORIGIN = 0x00000000, LENGTH = {}K
|
||
|
||
FILESYSTEM : ORIGIN = 0x{:08X}, LENGTH = {}K
|
||
|
||
/* for use with standard link.x */
|
||
/* SRAM0-3 (4×64K = 256K) contiguous with SRAM4 (16K @ 0x20040000),
|
||
* so 272K is one continuous region for stack/bss/heap. ML-DSA-44
|
||
* sign path on this firmware peaks near the limit; the extra 16K
|
||
* is the difference between wedging and not wedging on the
|
||
* `ctap23_pubkey_cred_params_picks_first_supported_alg` test. */
|
||
RAM : ORIGIN = 0x20000000, LENGTH = 272K
|
||
|
||
/* would be used with proper link.x */
|
||
/* needs changes to r0 (initialization code) */
|
||
/* SRAM0 : ORIGIN = 0x20000000, LENGTH = 64K */
|
||
/* SRAM1 : ORIGIN = 0x20010000, LENGTH = 64K */
|
||
/* SRAM2 : ORIGIN = 0x20020000, LENGTH = 64K */
|
||
/* SRAM3 : ORIGIN = 0x20030000, LENGTH = 64K */
|
||
|
||
/* CASPER SRAM regions */
|
||
/* SRAMX0: ORIGIN = 0x1400_0000, LENGTH = 4K /1* to 0x1400_0FFF *1/ */
|
||
/* SRAMX1: ORIGIN = 0x1400_4000, LENGTH = 4K /1* to 0x1400_4FFF *1/ */
|
||
|
||
/* USB1 SRAM regin */
|
||
/* USB1_SRAM : ORIGIN = 0x40100000, LENGTH = 16K */
|
||
|
||
/* To define our own USB RAM section in one regular */
|
||
/* RAM, probably easiest to shorten length of RAM */
|
||
/* above, and use this freed RAM section */
|
||
|
||
}}
|
||
",
|
||
parameters.filesystem_boundary / 1024, // code size in KB
|
||
parameters.filesystem_boundary, // Start address of filesystem
|
||
630 - parameters.filesystem_boundary / 1024, // filesystem size in KB
|
||
)
|
||
.expect("Could not write memory.x");
|
||
}
|
||
|
||
fn main() -> Result<(), Box<dyn error::Error>> {
|
||
println!("cargo:rerun-if-changed=config/src/lib.rs");
|
||
println!("cargo:rerun-if-changed=cfg.toml");
|
||
|
||
let out_dir = env::var("OUT_DIR").expect("No out dir");
|
||
println!("cargo:rustc-link-search={out_dir}");
|
||
|
||
// We would like to put configuration variables in cfg.toml, but due to a Cargo bug,
|
||
// the serde feature flags will get merged with solo-bee's serde, causing build issues.
|
||
// So this will remain here until the Cargo bug gets fixed.
|
||
|
||
// let config = fs::read_to_string("cfg.toml")?;
|
||
// let config: Config = toml::from_str(&config)?;
|
||
|
||
// Hardcode until cargo issue gets fixed.
|
||
let config = Config {
|
||
parameters: Parameters {
|
||
filesystem_boundary: 0x8_0000,
|
||
},
|
||
};
|
||
|
||
let linker_script_file = Path::new(&out_dir).join("memory.x");
|
||
let mut linker_script_file = File::create(&linker_script_file).expect("Could not create file");
|
||
|
||
let dest_path = Path::new(&out_dir).join("build_constants.rs");
|
||
let mut f = File::create(&dest_path).expect("Could not create file");
|
||
|
||
let hash_long_cmd = Command::new("git")
|
||
.args(["rev-parse", "HEAD"])
|
||
.output()
|
||
.unwrap()
|
||
.stdout;
|
||
let hash_short_cmd = Command::new("git")
|
||
.args(["rev-parse", "--short", "HEAD"])
|
||
.output()
|
||
.unwrap()
|
||
.stdout;
|
||
|
||
let hash_long = str::from_utf8(&hash_long_cmd).unwrap();
|
||
let hash_short = str::from_utf8(&hash_short_cmd).unwrap();
|
||
|
||
writeln!(&mut f, "pub mod build_constants {{").expect("Could not write build_constants.rs.");
|
||
add_build_variable!(&mut f, "CARGO_PKG_VERSION_MAJOR", u8);
|
||
add_build_variable!(&mut f, "CARGO_PKG_VERSION_MINOR", u16);
|
||
add_build_variable!(&mut f, "CARGO_PKG_VERSION_PATCH", u8);
|
||
|
||
add_build_variable!(&mut f, "CARGO_PKG_HASH", hash_long);
|
||
add_build_variable!(&mut f, "CARGO_PKG_HASH_SHORT", hash_short);
|
||
|
||
// Add integer version of the version number
|
||
let major: u32 = str::parse(env!("CARGO_PKG_VERSION_MAJOR")).unwrap();
|
||
let minor: u32 = str::parse(env!("CARGO_PKG_VERSION_MINOR")).unwrap();
|
||
let patch: u32 = str::parse(env!("CARGO_PKG_VERSION_PATCH")).unwrap();
|
||
|
||
if major >= 1024 || minor > 9999 || patch >= 64 {
|
||
panic!("config.firmware.product can at most be 1023.9999.63 for versions in customer data");
|
||
}
|
||
|
||
let version_to_check: u32 = (major << 22) | (minor << 6) | patch;
|
||
|
||
add_build_variable!(&mut f, "CARGO_PKG_VERSION", version_to_check, u32);
|
||
|
||
add_build_variable!(
|
||
&mut f,
|
||
"CONFIG_FILESYSTEM_BOUNDARY",
|
||
config.parameters.filesystem_boundary,
|
||
usize
|
||
);
|
||
|
||
writeln!(&mut f, "}}").expect("Could not write build_constants.rs.");
|
||
|
||
write_memory_x(&mut linker_script_file, &config.parameters);
|
||
|
||
Ok(())
|
||
}
|