2024-05-31 09:11:45 -05:00
|
|
|
mod dev;
|
|
|
|
|
2023-05-06 06:50:24 -05:00
|
|
|
use {
|
2024-11-08 08:04:10 -06:00
|
|
|
core::fmt::Write as _,
|
2023-07-17 09:36:39 -05:00
|
|
|
derive_more::Display,
|
2024-05-31 09:11:45 -05:00
|
|
|
dev::Package,
|
2023-11-11 08:45:45 -06:00
|
|
|
error_stack::{bail, report, Context, Report, Result, ResultExt},
|
2023-05-06 06:50:24 -05:00
|
|
|
fatfs::{FileSystem, FormatVolumeOptions, FsOptions, ReadWriteSeek},
|
2024-02-15 14:21:00 -06:00
|
|
|
std::{
|
|
|
|
fs::{self, File},
|
|
|
|
io::{self, Write},
|
|
|
|
path::Path,
|
2024-10-12 07:35:49 -05:00
|
|
|
process::{exit, Command, Stdio},
|
2024-02-15 14:21:00 -06:00
|
|
|
},
|
2024-05-31 10:43:40 -05:00
|
|
|
toml::Value,
|
2023-05-06 06:50:24 -05:00
|
|
|
};
|
2023-03-30 16:43:04 -05:00
|
|
|
|
|
|
|
fn main() -> Result<(), Error> {
|
|
|
|
let mut args = std::env::args();
|
|
|
|
args.next();
|
2023-06-13 21:03:09 -05:00
|
|
|
|
2024-09-30 15:45:57 -05:00
|
|
|
log::set_logger(&hblang::Logger).unwrap();
|
|
|
|
log::set_max_level(log::LevelFilter::Error);
|
|
|
|
|
2023-03-30 16:43:04 -05:00
|
|
|
match args.next().as_deref() {
|
|
|
|
Some("build" | "b") => {
|
|
|
|
let mut release = false;
|
2024-08-31 13:34:14 -05:00
|
|
|
let mut debuginfo = false;
|
2023-03-30 16:43:04 -05:00
|
|
|
let mut target = Target::X86_64;
|
2024-11-23 18:21:39 -06:00
|
|
|
let mut tests = false;
|
2023-03-30 16:43:04 -05:00
|
|
|
for arg in args {
|
|
|
|
if arg == "-r" || arg == "--release" {
|
|
|
|
release = true;
|
2024-08-31 13:34:14 -05:00
|
|
|
} else if arg == "-d" || arg == "--debuginfo" {
|
|
|
|
debuginfo = true;
|
2023-07-17 09:36:39 -05:00
|
|
|
} else if arg == "rv64" || arg == "riscv64" || arg == "riscv64-virt" {
|
2023-03-30 16:43:04 -05:00
|
|
|
target = Target::Riscv64Virt;
|
2023-07-19 10:55:58 -05:00
|
|
|
} else if arg == "arm64" || arg == "aarch64" || arg == "aarch64-virt" {
|
2023-07-13 22:41:09 -05:00
|
|
|
target = Target::Aarch64;
|
2024-10-19 13:45:41 -05:00
|
|
|
} else if arg == "avx2" {
|
|
|
|
target = Target::X86_64Avx2;
|
2024-11-23 18:21:39 -06:00
|
|
|
} else if arg == "--ktest" {
|
|
|
|
tests = true;
|
2023-07-17 09:36:39 -05:00
|
|
|
} else {
|
|
|
|
return Err(report!(Error::InvalidSubCom));
|
2023-07-13 22:41:09 -05:00
|
|
|
}
|
2023-03-30 16:43:04 -05:00
|
|
|
}
|
2022-08-03 02:11:51 -05:00
|
|
|
|
2024-11-23 18:21:39 -06:00
|
|
|
build(release, target, debuginfo, tests).change_context(Error::Build)
|
2023-03-30 16:43:04 -05:00
|
|
|
}
|
2024-11-23 18:21:39 -06:00
|
|
|
// Some("test" | "t") => {
|
|
|
|
// let mut release = false;
|
|
|
|
// let mut debuginfo = false;
|
|
|
|
// let mut target = Target::X86_64;
|
|
|
|
// for arg in args {
|
|
|
|
// if arg == "-r" || arg == "--release" {
|
|
|
|
// release = true;
|
|
|
|
// } else if arg == "-d" || arg == "--debuginfo" {
|
|
|
|
// debuginfo = true;
|
|
|
|
// } else if arg == "rv64" || arg == "riscv64" || arg == "riscv64-virt" {
|
|
|
|
// target = Target::Riscv64Virt;
|
|
|
|
// } else if arg == "arm64" || arg == "aarch64" || arg == "aarch64-virt" {
|
|
|
|
// target = Target::Aarch64;
|
|
|
|
// } else if arg == "avx2" {
|
|
|
|
// target = Target::X86_64Avx2;
|
|
|
|
// } else {
|
|
|
|
// return Err(report!(Error::InvalidSubCom));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// test(release, target, debuginfo).change_context(Error::Build)
|
|
|
|
// }
|
2023-03-30 16:43:04 -05:00
|
|
|
Some("run" | "r") => {
|
|
|
|
let mut release = false;
|
2024-08-31 13:34:14 -05:00
|
|
|
let mut debuginfo = false;
|
2023-03-30 16:43:04 -05:00
|
|
|
let mut target = Target::X86_64;
|
2024-11-23 18:21:39 -06:00
|
|
|
let mut tests = false;
|
2024-10-12 09:02:55 -05:00
|
|
|
let mut do_accel = true;
|
2023-03-30 16:43:04 -05:00
|
|
|
for arg in args {
|
|
|
|
if arg == "-r" || arg == "--release" {
|
|
|
|
release = true;
|
2024-08-31 13:34:14 -05:00
|
|
|
} else if arg == "-d" || arg == "--debuginfo" {
|
|
|
|
debuginfo = true;
|
2023-07-17 09:36:39 -05:00
|
|
|
} else if arg == "rv64" || arg == "riscv64" || arg == "riscv64-virt" {
|
2023-03-30 16:43:04 -05:00
|
|
|
target = Target::Riscv64Virt;
|
2023-07-19 10:55:58 -05:00
|
|
|
} else if arg == "arm64" || arg == "aarch64" || arg == "aarch64-virt" {
|
2023-07-13 22:41:09 -05:00
|
|
|
target = Target::Aarch64;
|
2024-10-12 09:02:55 -05:00
|
|
|
} else if arg == "--noaccel" {
|
|
|
|
do_accel = false;
|
2024-10-19 13:45:41 -05:00
|
|
|
} else if arg == "avx2" {
|
|
|
|
target = Target::X86_64Avx2;
|
2024-11-23 18:21:39 -06:00
|
|
|
} else if arg == "--ktest" {
|
2024-12-04 04:21:24 -06:00
|
|
|
tests = true;
|
2023-07-17 09:36:39 -05:00
|
|
|
} else {
|
|
|
|
return Err(report!(Error::InvalidSubCom));
|
2023-07-13 22:41:09 -05:00
|
|
|
}
|
2023-03-30 16:43:04 -05:00
|
|
|
}
|
2022-08-03 02:11:51 -05:00
|
|
|
|
2024-11-23 18:21:39 -06:00
|
|
|
build(release, target, debuginfo, tests)?;
|
2024-10-12 09:02:55 -05:00
|
|
|
run(release, target, do_accel)
|
2023-03-30 16:43:04 -05:00
|
|
|
}
|
|
|
|
Some("help" | "h") => {
|
|
|
|
println!(concat!(
|
|
|
|
"AbleOS RepBuild\n",
|
|
|
|
"Subcommands:\n",
|
|
|
|
" build (b): Build a bootable disk image\n",
|
|
|
|
" help (h): Print this message\n",
|
|
|
|
" run (r): Build and run AbleOS in QEMU\n\n",
|
|
|
|
"Options for build and run:\n",
|
2024-10-12 09:02:55 -05:00
|
|
|
" -r / --release: build in release mode\n",
|
|
|
|
" -d / --debuginfo: build with debug info\n",
|
|
|
|
" --noaccel: run without acceleration (e.g, no kvm)\n",
|
2024-11-23 18:21:39 -06:00
|
|
|
" --ktest: Enables tests via ktest\n",
|
2024-10-25 10:37:38 -05:00
|
|
|
"[ rv64 / riscv64 / riscv64-virt / aarch64 / arm64 / aarch64-virt / avx2 ]: sets target"
|
2023-03-30 16:43:04 -05:00
|
|
|
),);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
_ => Err(report!(Error::InvalidSubCom)),
|
|
|
|
}
|
2022-08-03 02:11:51 -05:00
|
|
|
}
|
|
|
|
|
2024-05-31 10:43:40 -05:00
|
|
|
fn get_path_without_boot_prefix(val: &Value) -> Option<&str> {
|
|
|
|
val.as_str()?.split("boot:///").last()
|
|
|
|
}
|
|
|
|
|
2023-03-30 16:43:04 -05:00
|
|
|
fn get_fs() -> Result<FileSystem<impl ReadWriteSeek>, io::Error> {
|
2024-01-18 02:36:24 -06:00
|
|
|
let filename = "sysdata/system_config.toml";
|
|
|
|
|
2024-05-05 10:48:37 -05:00
|
|
|
let mut img = File::options()
|
|
|
|
.read(true)
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.open(Path::new("target/disk.img"))?;
|
|
|
|
|
|
|
|
img.set_len(1024 * 1024 * 64)?;
|
|
|
|
|
|
|
|
fatfs::format_volume(&mut img, FormatVolumeOptions::new())?;
|
|
|
|
|
|
|
|
let fs = FileSystem::new(img, FsOptions::new())?;
|
|
|
|
|
2024-01-18 02:36:24 -06:00
|
|
|
// Read the contents of the file using a `match` block
|
|
|
|
// to return the `data: Ok(c)` as a `String`
|
|
|
|
// or handle any `errors: Err(_)`.
|
|
|
|
let contents = match fs::read_to_string(filename) {
|
|
|
|
// If successful return the files text as `contents`.
|
|
|
|
// `c` is a local variable.
|
|
|
|
Ok(c) => c,
|
|
|
|
// Handle the `error` case.
|
|
|
|
Err(_) => {
|
|
|
|
// Write `msg` to `stderr`.
|
|
|
|
eprintln!("Could not read file `{}`", filename);
|
|
|
|
// Exit the program with exit code `1`.
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
use toml::Value;
|
|
|
|
|
|
|
|
let mut limine_str = String::new();
|
|
|
|
|
|
|
|
let mut data: Value = toml::from_str(&contents).unwrap();
|
|
|
|
let boot_table = data.get_mut("boot");
|
|
|
|
let limine_table = boot_table.unwrap().get_mut("limine").unwrap();
|
|
|
|
let default_entry = limine_table.get("default_entry").unwrap();
|
|
|
|
let timeout = limine_table.get("timeout").unwrap();
|
|
|
|
let interface_resolution = limine_table.get("interface_resolution").unwrap();
|
|
|
|
let verbose = limine_table.get("verbose").unwrap();
|
|
|
|
let term_wallpaper = limine_table.get("term_wallpaper").unwrap();
|
|
|
|
|
|
|
|
let vb_post = match verbose.as_bool().unwrap() {
|
|
|
|
true => "yes",
|
|
|
|
false => "no",
|
|
|
|
};
|
|
|
|
let term_background = limine_table
|
|
|
|
.get("term_backdrop")
|
|
|
|
.unwrap_or(&Value::Integer(0));
|
|
|
|
let base = format!(
|
|
|
|
"DEFAULT_ENTRY={}
|
|
|
|
TIMEOUT={}
|
|
|
|
VERBOSE={}
|
|
|
|
INTERFACE_RESOLUTION={}
|
|
|
|
# Terminal related settings
|
|
|
|
TERM_WALLPAPER={}
|
|
|
|
TERM_BACKDROP={}
|
|
|
|
",
|
|
|
|
default_entry,
|
|
|
|
timeout,
|
|
|
|
vb_post,
|
|
|
|
interface_resolution,
|
|
|
|
term_wallpaper.as_str().unwrap(),
|
|
|
|
term_background
|
|
|
|
);
|
2024-05-05 10:48:37 -05:00
|
|
|
|
|
|
|
// Copy the term_wallpaper to the image
|
2024-05-31 10:43:40 -05:00
|
|
|
let term_wallpaper_path = get_path_without_boot_prefix(term_wallpaper).unwrap();
|
2024-05-05 10:48:37 -05:00
|
|
|
copy_file_to_img(&format!("sysdata/{}", term_wallpaper_path), &fs);
|
|
|
|
|
2024-01-18 02:36:24 -06:00
|
|
|
limine_str.push_str(&base);
|
|
|
|
|
|
|
|
let boot_entries = limine_table.as_table_mut().unwrap();
|
|
|
|
let mut real_boot_entries = boot_entries.clone();
|
|
|
|
for (key, value) in boot_entries.into_iter() {
|
|
|
|
if !value.is_table() {
|
|
|
|
real_boot_entries.remove(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (name, mut value) in real_boot_entries {
|
|
|
|
let comment = value.get("comment").unwrap();
|
|
|
|
let protocol = value.get("protocol").unwrap();
|
|
|
|
let resolution = value.get("resolution").unwrap();
|
|
|
|
let kernel_path = value.get("kernel_path").unwrap();
|
|
|
|
let kernel_cmdline = value.get("kernel_cmdline").unwrap();
|
|
|
|
|
|
|
|
let text_entry = format!(
|
|
|
|
"
|
|
|
|
:{}
|
|
|
|
COMMENT={}
|
|
|
|
PROTOCOL={}
|
|
|
|
RESOLUTION={}
|
|
|
|
KERNEL_PATH={}
|
|
|
|
KERNEL_CMDLINE={}
|
|
|
|
|
|
|
|
",
|
|
|
|
name,
|
|
|
|
comment.as_str().unwrap(),
|
|
|
|
protocol.as_str().unwrap(),
|
|
|
|
resolution.as_str().unwrap(),
|
|
|
|
kernel_path.as_str().unwrap(),
|
|
|
|
kernel_cmdline,
|
|
|
|
);
|
|
|
|
|
|
|
|
limine_str.push_str(&text_entry);
|
|
|
|
|
|
|
|
let modules = value.get_mut("modules").unwrap().as_table_mut().unwrap();
|
|
|
|
// let mut real_modules = modules.clone();
|
|
|
|
|
2024-11-08 08:04:10 -06:00
|
|
|
let mut errors = String::new();
|
|
|
|
let mut out = Vec::new();
|
|
|
|
|
2024-10-25 10:37:38 -05:00
|
|
|
modules
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_, value)| -> Result<(), io::Error> {
|
|
|
|
if value.is_table() {
|
|
|
|
let path = get_path_without_boot_prefix(
|
|
|
|
value.get("path").expect("You must have `path` as a value"),
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.split(".")
|
|
|
|
.next()
|
|
|
|
.unwrap();
|
|
|
|
let p = Package::load_from_file(
|
|
|
|
format!("sysdata/programs/{}/meta.toml", path).to_owned(),
|
|
|
|
);
|
2024-11-08 08:04:10 -06:00
|
|
|
match p.build(&mut out) {
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(_) => {
|
|
|
|
writeln!(errors, "========= while compiling {} =========", path)
|
|
|
|
.unwrap();
|
|
|
|
errors.push_str(core::str::from_utf8(&out).expect("no"));
|
|
|
|
out.clear();
|
|
|
|
}
|
|
|
|
}
|
2024-10-25 10:37:38 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.for_each(drop);
|
2024-11-08 08:04:10 -06:00
|
|
|
|
|
|
|
if !errors.is_empty() {
|
2024-11-08 10:16:24 -06:00
|
|
|
let _ = writeln!(errors, "!!! STOPPING DUE TO PREVIOUS ERRORS !!!");
|
2024-11-08 08:04:10 -06:00
|
|
|
std::eprint!("{errors}");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-05-31 10:31:04 -05:00
|
|
|
modules.into_iter().for_each(|(_key, value)| {
|
2024-01-18 02:36:24 -06:00
|
|
|
if value.is_table() {
|
2024-05-05 05:52:49 -05:00
|
|
|
let path = value.get("path").expect("You must have `path` as a value");
|
|
|
|
let default_value = Value::String("".into());
|
|
|
|
let cmd_line = value.get("cmd_line").unwrap_or(&default_value);
|
|
|
|
|
2024-01-18 02:36:24 -06:00
|
|
|
let a = format!(
|
|
|
|
" MODULE_PATH={}
|
|
|
|
MODULE_CMDLINE={}\n\n",
|
|
|
|
path.as_str().unwrap(),
|
|
|
|
cmd_line
|
|
|
|
);
|
|
|
|
limine_str.push_str(&a);
|
|
|
|
}
|
2024-05-31 10:31:04 -05:00
|
|
|
});
|
2023-03-30 16:43:04 -05:00
|
|
|
|
2024-05-05 10:48:37 -05:00
|
|
|
// Copy modules into the test_programs directory
|
2024-05-31 10:31:04 -05:00
|
|
|
modules.into_iter().for_each(|(_key, value)| {
|
2024-05-05 10:48:37 -05:00
|
|
|
if value.is_table() {
|
2024-05-31 10:43:40 -05:00
|
|
|
let path = get_path_without_boot_prefix(
|
|
|
|
value
|
|
|
|
.get("path")
|
|
|
|
.expect("You must have a `path` as a value"),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-05-31 13:31:06 -05:00
|
|
|
let fpath = format!("target/programs/{}", path);
|
2024-05-05 10:48:37 -05:00
|
|
|
copy_file_to_img(&fpath, &fs);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-01-18 02:36:24 -06:00
|
|
|
|
2023-03-30 16:43:04 -05:00
|
|
|
let bootdir = fs.root_dir().create_dir("efi")?.create_dir("boot")?;
|
|
|
|
|
2024-01-18 02:36:24 -06:00
|
|
|
let mut f = fs.root_dir().create_file("limine.cfg")?;
|
2024-09-13 16:41:31 -05:00
|
|
|
let _ = f.write(limine_str.as_bytes())?;
|
2024-01-18 02:36:24 -06:00
|
|
|
drop(f);
|
|
|
|
|
2023-03-30 16:43:04 -05:00
|
|
|
io::copy(
|
|
|
|
&mut File::open("limine/BOOTX64.EFI")
|
2023-08-29 18:12:40 -05:00
|
|
|
.map_err(Report::from)
|
2023-11-11 08:45:45 -06:00
|
|
|
.attach_printable("Copying Limine (x86_64): have you pulled the submodule?")?,
|
2023-03-30 16:43:04 -05:00
|
|
|
&mut bootdir.create_file("bootx64.efi")?,
|
|
|
|
)?;
|
|
|
|
|
2023-07-17 00:36:06 -05:00
|
|
|
io::copy(
|
|
|
|
&mut File::open("limine/BOOTAA64.EFI")
|
2023-08-29 18:12:40 -05:00
|
|
|
.map_err(Report::from)
|
2023-11-13 23:51:30 -06:00
|
|
|
.attach_printable("Copying Limine (ARM): have you pulled the submodule?")?,
|
2023-07-17 00:36:06 -05:00
|
|
|
&mut bootdir.create_file("bootaa64.efi")?,
|
|
|
|
)?;
|
2023-10-29 07:27:10 -05:00
|
|
|
|
2023-03-30 16:43:04 -05:00
|
|
|
drop(bootdir);
|
|
|
|
Ok(fs)
|
2022-08-03 02:11:51 -05:00
|
|
|
}
|
|
|
|
|
2024-05-05 10:48:37 -05:00
|
|
|
fn copy_file_to_img(fpath: &str, fs: &FileSystem<File>) {
|
|
|
|
let path = Path::new(fpath);
|
2024-05-31 13:31:06 -05:00
|
|
|
// println!("{path:?}");
|
2024-05-05 10:48:37 -05:00
|
|
|
io::copy(
|
|
|
|
&mut File::open(path).expect(&format!("Could not open file {fpath}")),
|
|
|
|
&mut fs
|
|
|
|
.root_dir()
|
|
|
|
.create_file(&path.file_name().unwrap().to_string_lossy())
|
|
|
|
.expect("Unable to create file"),
|
|
|
|
)
|
|
|
|
.expect("Copy failed");
|
|
|
|
}
|
|
|
|
|
2024-11-23 18:21:39 -06:00
|
|
|
fn build(release: bool, target: Target, debuginfo: bool, tests: bool) -> Result<(), Error> {
|
2023-03-30 16:43:04 -05:00
|
|
|
let fs = get_fs().change_context(Error::Io)?;
|
|
|
|
let mut com = Command::new("cargo");
|
|
|
|
com.current_dir("kernel");
|
|
|
|
com.args(["b"]);
|
|
|
|
if release {
|
|
|
|
com.arg("-r");
|
|
|
|
}
|
2024-08-31 13:34:14 -05:00
|
|
|
if debuginfo {
|
|
|
|
com.env("RUSTFLAGS", "-Cdebug-assertions=true");
|
|
|
|
}
|
2023-03-30 16:43:04 -05:00
|
|
|
|
2024-11-23 18:21:39 -06:00
|
|
|
if tests {
|
|
|
|
com.args(["--features", "ktest"]);
|
|
|
|
}
|
|
|
|
|
2023-07-12 20:21:33 -05:00
|
|
|
if target == Target::Riscv64Virt {
|
|
|
|
com.args(["--target", "targets/riscv64-virt-ableos.json"]);
|
2022-08-07 07:35:55 -05:00
|
|
|
}
|
2023-07-13 22:41:09 -05:00
|
|
|
if target == Target::Aarch64 {
|
|
|
|
com.args(["--target", "targets/aarch64-virt-ableos.json"]);
|
|
|
|
}
|
2024-10-19 13:45:41 -05:00
|
|
|
if target == Target::X86_64Avx2 {
|
|
|
|
com.args(["--target", "targets/x86_64_v3-ableos.json"]);
|
|
|
|
}
|
2022-08-03 02:11:51 -05:00
|
|
|
|
2023-03-30 16:43:04 -05:00
|
|
|
match com.status() {
|
|
|
|
Ok(s) if s.code() != Some(0) => bail!(Error::Build),
|
|
|
|
Err(e) => bail!(report!(e).change_context(Error::Build)),
|
|
|
|
_ => (),
|
|
|
|
}
|
2022-08-03 02:11:51 -05:00
|
|
|
|
2023-09-20 12:26:36 -05:00
|
|
|
let mut path: String = "kernel".to_string();
|
2023-07-19 10:55:58 -05:00
|
|
|
let kernel_dir = match target {
|
2023-09-20 12:26:36 -05:00
|
|
|
Target::X86_64 => {
|
|
|
|
path.push_str("_x86-64");
|
|
|
|
"target/x86_64-ableos"
|
|
|
|
}
|
2024-10-19 13:45:41 -05:00
|
|
|
Target::X86_64Avx2 => {
|
|
|
|
path.push_str("_x86-64");
|
|
|
|
"target/x86_64_v3-ableos"
|
|
|
|
}
|
2023-07-19 10:55:58 -05:00
|
|
|
Target::Riscv64Virt => "target/riscv64-virt-ableos",
|
2023-09-20 12:26:36 -05:00
|
|
|
Target::Aarch64 => {
|
|
|
|
path.push_str("_aarch64");
|
|
|
|
"target/aarch64-virt-ableos"
|
|
|
|
}
|
2023-07-19 10:55:58 -05:00
|
|
|
};
|
2022-08-07 07:35:55 -05:00
|
|
|
|
2023-03-30 16:43:04 -05:00
|
|
|
(|| -> std::io::Result<_> {
|
|
|
|
io::copy(
|
|
|
|
&mut File::open(
|
2023-07-19 10:55:58 -05:00
|
|
|
Path::new(kernel_dir)
|
2023-03-30 16:43:04 -05:00
|
|
|
.join(if release { "release" } else { "debug" })
|
|
|
|
.join("kernel"),
|
|
|
|
)?,
|
2023-09-20 12:26:36 -05:00
|
|
|
&mut fs.root_dir().create_file(&path)?,
|
2023-03-30 16:43:04 -05:00
|
|
|
)
|
|
|
|
.map(|_| ())
|
|
|
|
})()
|
2023-08-29 18:12:40 -05:00
|
|
|
.map_err(Report::from)
|
2023-03-30 16:43:04 -05:00
|
|
|
.change_context(Error::Io)
|
|
|
|
}
|
2022-08-07 07:35:55 -05:00
|
|
|
|
2024-10-12 09:02:55 -05:00
|
|
|
fn run(release: bool, target: Target, do_accel: bool) -> Result<(), Error> {
|
2024-10-12 07:35:49 -05:00
|
|
|
let target_str = match target {
|
2024-10-19 13:45:41 -05:00
|
|
|
Target::X86_64 | Target::X86_64Avx2 => "qemu-system-x86_64",
|
2024-10-12 07:35:49 -05:00
|
|
|
Target::Riscv64Virt => "qemu-system-riscv64",
|
|
|
|
Target::Aarch64 => "qemu-system-aarch64",
|
2023-03-30 16:43:04 -05:00
|
|
|
};
|
2024-10-12 07:35:49 -05:00
|
|
|
let (mut com, mut com2) = (Command::new(target_str), Command::new(target_str));
|
2023-07-19 10:55:58 -05:00
|
|
|
let ovmf_path = fetch_ovmf(target);
|
2024-11-17 04:52:13 -06:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2024-10-12 09:02:55 -05:00
|
|
|
let accel = if do_accel {
|
|
|
|
let supported = String::from_utf8(
|
|
|
|
com2.args(["--accel", "help"])
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.stderr(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.unwrap()
|
|
|
|
.wait_with_output()
|
|
|
|
.unwrap()
|
|
|
|
.stdout,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let cpuid = raw_cpuid::CpuId::new();
|
|
|
|
let vmx = cpuid.get_feature_info().unwrap().has_vmx();
|
|
|
|
let svm = cpuid.get_svm_info().is_some();
|
|
|
|
|
|
|
|
if supported.contains("kvm") && (vmx || svm) {
|
|
|
|
"accel=kvm"
|
|
|
|
} else if cpuid
|
|
|
|
.get_processor_brand_string()
|
|
|
|
.filter(|a| a.as_str() == "GenuineIntel")
|
|
|
|
.is_some()
|
|
|
|
&& supported.contains("hax")
|
|
|
|
&& vmx
|
|
|
|
{
|
|
|
|
"accel=hax"
|
|
|
|
} else if supported.contains("whpx") {
|
|
|
|
"accel=whpx"
|
|
|
|
} else {
|
|
|
|
"accel=tcg"
|
|
|
|
}
|
2024-10-12 07:35:49 -05:00
|
|
|
} else {
|
|
|
|
"accel=tcg"
|
|
|
|
};
|
2024-11-17 04:52:13 -06:00
|
|
|
#[cfg(not(target_arch = "x86_64"))]
|
|
|
|
let accel = "accel=tcg";
|
2024-10-12 07:35:49 -05:00
|
|
|
|
2023-07-19 10:55:58 -05:00
|
|
|
match target {
|
2024-10-19 13:45:41 -05:00
|
|
|
Target::X86_64 | Target::X86_64Avx2 => {
|
2023-07-19 10:55:58 -05:00
|
|
|
#[rustfmt::skip]
|
|
|
|
com.args([
|
|
|
|
"-bios", &ovmf_path.change_context(Error::OvmfFetch)?,
|
2024-12-07 10:44:29 -06:00
|
|
|
//"-hda", "target/disk.img",
|
|
|
|
"-drive", "file=target/disk.img,index=0,if=ide,format=raw",
|
2024-09-19 14:40:10 -05:00
|
|
|
"-device", "vmware-svga",
|
2024-11-10 02:36:37 -06:00
|
|
|
// "-serial", "stdio",
|
2024-09-19 14:40:10 -05:00
|
|
|
"-m", "2G",
|
|
|
|
"-smp", "1",
|
2024-11-26 07:39:16 -06:00
|
|
|
"-audiodev",
|
|
|
|
"pa,id=speaker",
|
|
|
|
"-machine",
|
|
|
|
"pcspk-audiodev=speaker",
|
2024-10-12 07:35:49 -05:00
|
|
|
"-parallel", "none",
|
|
|
|
"-monitor", "none",
|
|
|
|
"-machine", accel,
|
2024-11-01 17:37:47 -05:00
|
|
|
"-cpu", "max",
|
2024-09-19 14:40:10 -05:00
|
|
|
"-device", "isa-debug-exit,iobase=0xf4,iosize=0x04",
|
2023-07-19 10:55:58 -05:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
Target::Riscv64Virt => {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
com.args([
|
|
|
|
"-M", "virt",
|
|
|
|
"-m", "128M",
|
|
|
|
"-serial", "stdio",
|
|
|
|
"-kernel",
|
|
|
|
if release {
|
|
|
|
"target/riscv64-virt-ableos/release/kernel"
|
|
|
|
} else {
|
|
|
|
"target/riscv64-virt-ableos/debug/kernel"
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
Target::Aarch64 => {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
com.args([
|
|
|
|
"-M", "virt",
|
2024-11-24 10:00:24 -06:00
|
|
|
"-cpu", "max",
|
2023-07-19 10:55:58 -05:00
|
|
|
"-device", "ramfb",
|
|
|
|
"-device", "qemu-xhci",
|
|
|
|
"-device", "usb-kbd",
|
|
|
|
"-m", "2G",
|
|
|
|
"-bios", &ovmf_path.change_context(Error::OvmfFetch)?,
|
|
|
|
"-drive", "file=target/disk.img,format=raw",
|
|
|
|
]);
|
2022-08-07 07:35:55 -05:00
|
|
|
}
|
2023-07-13 22:41:09 -05:00
|
|
|
}
|
|
|
|
|
2023-03-30 16:43:04 -05:00
|
|
|
match com
|
|
|
|
.status()
|
2023-08-29 18:12:40 -05:00
|
|
|
.map_err(Report::from)
|
2023-03-30 16:43:04 -05:00
|
|
|
.change_context(Error::ProcessSpawn)?
|
|
|
|
{
|
|
|
|
s if s.success() => Ok(()),
|
|
|
|
s => Err(report!(Error::Qemu(s.code()))),
|
2022-08-07 07:35:55 -05:00
|
|
|
}
|
2022-08-03 02:11:51 -05:00
|
|
|
}
|
|
|
|
|
2023-07-19 10:55:58 -05:00
|
|
|
fn fetch_ovmf(target: Target) -> Result<String, OvmfFetchError> {
|
|
|
|
let (ovmf_url, ovmf_path) = match target {
|
2024-10-19 13:45:41 -05:00
|
|
|
Target::X86_64 | Target::X86_64Avx2 => (
|
2023-07-19 10:55:58 -05:00
|
|
|
"https://retrage.github.io/edk2-nightly/bin/RELEASEX64_OVMF.fd",
|
|
|
|
"target/RELEASEX64_OVMF.fd",
|
|
|
|
),
|
|
|
|
Target::Riscv64Virt => return Err(OvmfFetchError::Empty.into()),
|
|
|
|
Target::Aarch64 => (
|
|
|
|
"https://retrage.github.io/edk2-nightly/bin/RELEASEAARCH64_QEMU_EFI.fd",
|
|
|
|
"target/RELEASEAARCH64_QEMU_EFI.fd",
|
|
|
|
),
|
|
|
|
};
|
2023-07-12 20:21:33 -05:00
|
|
|
|
2023-07-19 10:55:58 -05:00
|
|
|
let mut file = match std::fs::metadata(ovmf_path) {
|
2023-07-12 20:21:33 -05:00
|
|
|
Err(e) if e.kind() == std::io::ErrorKind::NotFound => std::fs::OpenOptions::new()
|
|
|
|
.create(true)
|
|
|
|
.write(true)
|
|
|
|
.read(true)
|
2023-07-19 10:55:58 -05:00
|
|
|
.open(ovmf_path)
|
2023-08-29 18:12:40 -05:00
|
|
|
.map_err(Report::from)
|
2023-07-12 20:21:33 -05:00
|
|
|
.change_context(OvmfFetchError::Io)?,
|
2023-07-19 10:55:58 -05:00
|
|
|
Ok(_) => return Ok(ovmf_path.to_owned()),
|
2023-07-12 20:21:33 -05:00
|
|
|
Err(e) => return Err(report!(e).change_context(OvmfFetchError::Io)),
|
|
|
|
};
|
2024-10-25 10:37:38 -05:00
|
|
|
let req = ureq::get(ovmf_url)
|
|
|
|
.call()
|
2023-08-29 18:12:40 -05:00
|
|
|
.map_err(Report::from)
|
2023-07-17 09:36:39 -05:00
|
|
|
.change_context(OvmfFetchError::Fetch)?;
|
2023-07-12 20:21:33 -05:00
|
|
|
|
2024-10-25 10:37:38 -05:00
|
|
|
std::io::copy(&mut req.into_reader(), &mut file)
|
2023-08-29 18:12:40 -05:00
|
|
|
.map_err(Report::from)
|
2023-07-17 09:36:39 -05:00
|
|
|
.change_context(OvmfFetchError::Io)?;
|
2023-07-12 20:21:33 -05:00
|
|
|
|
2023-07-19 10:55:58 -05:00
|
|
|
Ok(ovmf_path.to_owned())
|
2023-07-12 20:21:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Display)]
|
|
|
|
enum OvmfFetchError {
|
2024-09-13 16:41:31 -05:00
|
|
|
#[display("Failed to fetch OVMF package")]
|
2023-07-12 20:21:33 -05:00
|
|
|
Fetch,
|
2024-09-13 16:41:31 -05:00
|
|
|
#[display("No OVMF package available")]
|
2023-07-19 10:55:58 -05:00
|
|
|
Empty,
|
2024-09-13 16:41:31 -05:00
|
|
|
#[display("IO Error")]
|
2023-07-12 20:21:33 -05:00
|
|
|
Io,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Context for OvmfFetchError {}
|
|
|
|
|
2023-03-30 16:43:04 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
|
|
enum Target {
|
|
|
|
X86_64,
|
2024-10-19 13:45:41 -05:00
|
|
|
X86_64Avx2,
|
2023-03-30 16:43:04 -05:00
|
|
|
Riscv64Virt,
|
2023-07-13 22:41:09 -05:00
|
|
|
Aarch64,
|
2023-03-30 16:43:04 -05:00
|
|
|
}
|
2022-08-03 02:11:51 -05:00
|
|
|
|
2024-09-13 16:41:31 -05:00
|
|
|
#[allow(unused)]
|
2023-07-12 20:21:33 -05:00
|
|
|
#[derive(Debug, Display)]
|
2023-03-30 16:43:04 -05:00
|
|
|
enum Error {
|
2024-09-13 16:41:31 -05:00
|
|
|
#[display("Failed to build the kernel")]
|
2023-03-30 16:43:04 -05:00
|
|
|
Build,
|
2024-09-13 16:41:31 -05:00
|
|
|
#[display("Missing or invalid subcommand (available: build, run)")]
|
2023-03-30 16:43:04 -05:00
|
|
|
InvalidSubCom,
|
2024-09-13 16:41:31 -05:00
|
|
|
#[display("IO Error")]
|
2023-03-30 16:43:04 -05:00
|
|
|
Io,
|
2024-09-13 16:41:31 -05:00
|
|
|
#[display("Failed to spawn a process")]
|
2023-03-30 16:43:04 -05:00
|
|
|
ProcessSpawn,
|
2024-09-13 16:41:31 -05:00
|
|
|
#[display("Failed to fetch UEFI firmware")]
|
2023-07-12 20:21:33 -05:00
|
|
|
OvmfFetch,
|
2024-09-13 16:41:31 -05:00
|
|
|
#[display("Failed to assemble Holey Bytes code")]
|
2023-10-27 20:26:04 -05:00
|
|
|
Assembler,
|
2024-09-13 16:41:31 -05:00
|
|
|
#[display("QEMU Error: {}", "fmt_qemu_err(*_0)")]
|
2023-03-30 16:43:04 -05:00
|
|
|
Qemu(Option<i32>),
|
2022-08-03 02:11:51 -05:00
|
|
|
}
|
|
|
|
|
2023-03-30 16:43:04 -05:00
|
|
|
impl Context for Error {}
|
2023-07-12 20:21:33 -05:00
|
|
|
|
2024-09-13 16:41:31 -05:00
|
|
|
#[allow(dead_code)]
|
2023-07-12 20:21:33 -05:00
|
|
|
fn fmt_qemu_err(e: Option<i32>) -> impl Display {
|
|
|
|
struct W(Option<i32>);
|
|
|
|
impl Display for W {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
if let Some(c) = self.0 {
|
|
|
|
c.fmt(f)
|
|
|
|
} else {
|
|
|
|
f.write_str("Interrupted by signal")
|
2023-03-30 16:43:04 -05:00
|
|
|
}
|
2022-08-07 07:35:55 -05:00
|
|
|
}
|
|
|
|
}
|
2023-07-12 20:21:33 -05:00
|
|
|
|
|
|
|
W(e)
|
2022-08-03 02:11:51 -05:00
|
|
|
}
|