Просмотр исходного кода

feat(prototyper): read const in cfg.rs from toml file

Signed-off-by: Woshiluo Luo <woshiluo.luo@outlook.com>
Woshiluo Luo 1 месяц назад
Родитель
Сommit
3cce464902

+ 1 - 0
prototyper/prototyper/Cargo.toml

@@ -24,6 +24,7 @@ serde-device-tree = { git = "https://github.com/rustsbi/serde-device-tree", defa
 uart_xilinx = { git = "https://github.com/duskmoon314/uart-rs/" }
 xuantie-riscv = { git= "https://github.com/rustsbi/xuantie" }
 bouffalo-hal = { git = "https://github.com/rustsbi/bouffalo-hal", rev = "968b949", features = ["bl808"] }
+static-toml = "1"
 
 [[bin]]
 name = "rustsbi-prototyper"

+ 6 - 0
prototyper/prototyper/config_example.toml

@@ -0,0 +1,6 @@
+num_hart_max = 8
+len_stack_per_hart = 16384 # 16 * 1024
+heap_size = 32768 # 32 * 1024
+page_size = 4096
+log_level = "INFO"
+jump_address = 0x50000000

+ 17 - 7
prototyper/prototyper/src/cfg.rs

@@ -1,16 +1,26 @@
+use static_toml::static_toml;
+
 /// The address where the SBI link start.
 pub const SBI_LINK_START_ADDRESS: usize = 0x80000000;
+
+static_toml! {
+    const CONFIG = include_toml!("../../target/config.toml");
+}
+
 /// Maximum number of supported harts.
-pub const NUM_HART_MAX: usize = 8;
+pub const NUM_HART_MAX: usize = CONFIG.num_hart_max as usize;
 /// Stack size per hart (hardware thread) in bytes.
-pub const LEN_STACK_PER_HART: usize = 16 * 1024;
+pub const LEN_STACK_PER_HART: usize = CONFIG.len_stack_per_hart as usize;
 /// Heap Size of SBI firmware.
-pub const HEAP_SIZE: usize = 32 * 1024;
+pub const HEAP_SIZE: usize = CONFIG.heap_size as usize;
 /// Platform page size.
-pub const PAGE_SIZE: usize = 4096;
+pub const PAGE_SIZE: usize = CONFIG.page_size as usize;
+/// Log Level.
+pub const LOG_LEVEL: &'static str = CONFIG.log_level;
+/// Address for jump mode.
+#[cfg(feature = "jump")]
+pub const JUMP_ADDRESS: usize = CONFIG.jump_address as usize;
+
 /// TLB_FLUSH_LIMIT defines the TLB refresh range limit.
 /// If the TLB refresh range is greater than TLB_FLUSH_LIMIT, the entire TLB is refreshed.
 pub const TLB_FLUSH_LIMIT: usize = 4 * PAGE_SIZE;
-
-#[cfg(feature = "jump")]
-pub const JUMP_ADDRESS: usize = 0x50000000;

+ 2 - 4
prototyper/prototyper/src/sbi/logger.rs

@@ -7,10 +7,8 @@ pub struct Logger;
 impl Logger {
     /// Initialize the logger with log level from RUST_LOG env var or default to Info.
     pub fn init() -> Result<(), log::SetLoggerError> {
-        // Set max log level from RUST_LOG env var if present, otherwise use Info
-        let max_level = option_env!("RUST_LOG")
-            .and_then(|s| LevelFilter::from_str(s).ok())
-            .unwrap_or(LevelFilter::Info);
+        // Set max log level from LOG_LEVEL from config file, otherwise use Info
+        let max_level = LevelFilter::from_str(crate::cfg::LOG_LEVEL).unwrap_or(LevelFilter::Info);
 
         log::set_max_level(max_level);
         log::set_logger(&Logger)

+ 2 - 0
xtask/Cargo.toml

@@ -10,3 +10,5 @@ publish = false
 clap = { version = "4.5.4", features = ["derive", "env", "suggestions"] }
 log = "0.4.21"
 clap-verbosity-flag = "3.0.2"
+serde = "1.0"
+toml = "0.8.20"

+ 18 - 5
xtask/src/prototyper.rs

@@ -22,8 +22,8 @@ pub struct PrototyperArg {
     #[clap(long)]
     pub jump: bool,
 
-    #[clap(long, default_value = "INFO")]
-    pub log_level: String,
+    #[clap(long, short = 'c', required = true)]
+    pub config_file: String,
 }
 
 #[must_use]
@@ -33,13 +33,27 @@ pub fn run(arg: &PrototyperArg) -> Option<ExitStatus> {
     let fdt = arg.fdt.clone();
     let payload = arg.payload.clone();
     let jump = arg.jump;
+
     let current_dir = env::current_dir();
-    let target_dir = current_dir
+    let raw_target_dir = current_dir
         .as_ref()
         .unwrap()
-        .join("target")
+        .join("target");
+    let target_dir = raw_target_dir
         .join(arch)
         .join("release");
+    let target_config_toml = raw_target_dir.join("config.toml");
+
+    if fs::exists(&target_config_toml).ok()? {
+        info!("Delete old config");
+        fs::remove_file(&target_config_toml).ok()?;
+    }
+
+    info!("Copy config");
+    fs::copy(
+        &arg.config_file,
+        target_config_toml
+    ).ok()?;
 
     info!("Building Protoyper");
     cargo::Cargo::new("build")
@@ -59,7 +73,6 @@ pub fn run(arg: &PrototyperArg) -> Option<ExitStatus> {
         .optional(jump, |cargo| {
             cargo.features(["jump".to_string()])
         })
-        .env("RUST_LOG", &arg.log_level)
         .release()
         .status()
         .ok()?;