diff --git a/ch2/os/build.rs b/ch2/os/build.rs new file mode 100644 index 0000000..026b308 --- /dev/null +++ b/ch2/os/build.rs @@ -0,0 +1,69 @@ +use std::fs::{read_dir, File}; +use std::io::{Result, Write}; + +fn main() { + println!("cargo:rerun-if-changed=../user/src/"); + println!("cargo:rerun-if-changed={}", TARGET_PATH); + insert_app_data().unwrap(); +} + +static TARGET_PATH: &str = "../user/target/riscv64gc-unknown-none-elf/release/"; + +fn insert_app_data() -> Result<()> { + let mut f = File::create("src/link_app.S").unwrap(); + let mut apps: Vec<_> = read_dir("../user/src/bin") + .unwrap() + .into_iter() + .map(|dir_entry| { + let mut name_with_ext = dir_entry.unwrap().file_name().into_string().unwrap(); + name_with_ext.drain(name_with_ext.find('.').unwrap()..name_with_ext.len()); + name_with_ext + }) + .collect(); + apps.sort(); + + + /// + /// .align 3 表示接下来的数据或代码 使用2^3 8字节对齐 + /// .section .data 下面属于data段 + /// .global _num_app 定义一个全局符号 _num_app + /// _num_app: _num_app的位置的数据 + /// .quad 5 用来当做matedata 8字节的整数 表示有5个元素 + /// .quad app_0_start 依次存储每个app的开始的位置 + /// .quad app_1_start + /// .quad app_2_start + /// .quad app_3_start + /// .quad app_4_start + /// .quad app_4_end + writeln!( + f, + r#" + .align 3 + .section .data + .global _num_app +_num_app: + .quad {}"#, + apps.len() + )?; + + for i in 0..apps.len() { + writeln!(f, r#" .quad app_{}_start"#, i)?; + } + writeln!(f, r#" .quad app_{}_end"#, apps.len() - 1)?; + + for (idx, app) in apps.iter().enumerate() { + println!("app_{}: {}", idx, app); + writeln!( + f, + r#" + .section .data + .global app_{0}_start + .global app_{0}_end +app_{0}_start: + .incbin "{2}{1}.bin" +app_{0}_end:"#, + idx, app, TARGET_PATH + )?; + } + Ok(()) +}