2023-05-06 06:50:24 -05:00
|
|
|
use {
|
2023-07-17 09:36:39 -05:00
|
|
|
derive_more::Display,
|
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},
|
2023-07-19 10:55:58 -05:00
|
|
|
std::{fmt::Display, fs::File, io, path::Path, process::Command},
|
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
|
|
|
|
2023-07-08 23:21:27 -05:00
|
|
|
// let disk_meta = fs::metadata("target/disk.img").unwrap();
|
|
|
|
// let config_meta = fs::metadata("system.toml").unwrap();
|
2023-06-13 21:03:09 -05:00
|
|
|
|
2023-07-08 23:21:27 -05:00
|
|
|
// if disk_meta.modified().unwrap() < config_meta.modified().unwrap() {
|
|
|
|
// // TODO: work on adding in system.toml support
|
|
|
|
// // TODO: rebuild the disk
|
|
|
|
// }
|
2023-03-30 16:43:04 -05:00
|
|
|
|
|
|
|
match args.next().as_deref() {
|
|
|
|
Some("build" | "b") => {
|
|
|
|
let mut release = false;
|
|
|
|
let mut target = Target::X86_64;
|
|
|
|
for arg in args {
|
|
|
|
if arg == "-r" || arg == "--release" {
|
|
|
|
release = 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;
|
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
|
|
|
|
2023-10-27 20:26:04 -05:00
|
|
|
assemble()?;
|
2023-03-30 16:43:04 -05:00
|
|
|
build(release, target).change_context(Error::Build)
|
|
|
|
}
|
|
|
|
Some("run" | "r") => {
|
|
|
|
let mut release = false;
|
|
|
|
let mut target = Target::X86_64;
|
|
|
|
for arg in args {
|
|
|
|
if arg == "-r" || arg == "--release" {
|
|
|
|
release = 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;
|
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
|
|
|
|
2023-10-27 20:26:04 -05:00
|
|
|
assemble()?;
|
2023-03-30 16:43:04 -05:00
|
|
|
build(release, target)?;
|
|
|
|
run(release, target)
|
|
|
|
}
|
|
|
|
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",
|
|
|
|
" -r: build in release mode",
|
|
|
|
" [target]: sets target"
|
|
|
|
),);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
_ => Err(report!(Error::InvalidSubCom)),
|
|
|
|
}
|
2022-08-03 02:11:51 -05:00
|
|
|
}
|
|
|
|
|
2023-10-27 20:26:04 -05:00
|
|
|
fn assemble() -> Result<(), Error> {
|
2023-11-11 08:45:45 -06:00
|
|
|
match std::fs::create_dir("target/test-programs") {
|
2023-10-27 20:26:04 -05:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => (),
|
|
|
|
Err(e) => return Err(Report::new(e).change_context(Error::Io)),
|
|
|
|
}
|
|
|
|
|
2023-11-11 08:45:45 -06:00
|
|
|
for entry in std::fs::read_dir("sysdata/test-programs")
|
2023-10-27 20:26:04 -05:00
|
|
|
.map_err(Report::from)
|
|
|
|
.change_context(Error::Io)?
|
|
|
|
{
|
|
|
|
let entry = entry.map_err(Report::from).change_context(Error::Io)?;
|
2023-11-11 08:45:45 -06:00
|
|
|
if !entry
|
|
|
|
.file_type()
|
|
|
|
.map_err(Report::from)
|
|
|
|
.change_context(Error::Io)?
|
|
|
|
.is_file()
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-10-27 20:26:04 -05:00
|
|
|
let name = entry.file_name();
|
|
|
|
let name = name.to_string_lossy();
|
|
|
|
let name = name.trim_end_matches(".rhai");
|
|
|
|
|
|
|
|
let mut out = File::options()
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
2023-11-11 08:45:45 -06:00
|
|
|
.open(Path::new("target/test-programs").join(format!("{name}.hbf")))
|
2023-10-27 20:26:04 -05:00
|
|
|
.map_err(Report::from)
|
|
|
|
.change_context(Error::Io)?;
|
|
|
|
|
|
|
|
out.set_len(0)
|
|
|
|
.map_err(Report::from)
|
|
|
|
.change_context(Error::Io)?;
|
|
|
|
|
|
|
|
hbasm::assembler(&mut out, |engine| engine.run_file(entry.path()))
|
|
|
|
.map_err(|e| report!(Error::Assembler).attach_printable(e.to_string()))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-30 16:43:04 -05:00
|
|
|
fn get_fs() -> Result<FileSystem<impl ReadWriteSeek>, io::Error> {
|
|
|
|
let mut img = File::options()
|
|
|
|
.read(true)
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
2023-07-20 05:42:48 -05:00
|
|
|
.open(Path::new("target/disk.img"))?;
|
2023-03-30 16:43:04 -05:00
|
|
|
|
|
|
|
img.set_len(1024 * 1024 * 64)?;
|
|
|
|
|
|
|
|
fatfs::format_volume(&mut img, FormatVolumeOptions::new())?;
|
|
|
|
|
|
|
|
let fs = FileSystem::new(img, FsOptions::new())?;
|
|
|
|
let bootdir = fs.root_dir().create_dir("efi")?.create_dir("boot")?;
|
|
|
|
|
|
|
|
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-07-17 00:36:06 -05:00
|
|
|
.attach_printable(
|
2023-11-11 08:45:45 -06:00
|
|
|
"Copying Limine (ARM): have you pulled the submodule?",
|
2023-07-17 00:36:06 -05:00
|
|
|
)?,
|
|
|
|
&mut bootdir.create_file("bootaa64.efi")?,
|
|
|
|
)?;
|
|
|
|
|
2023-11-11 08:45:45 -06:00
|
|
|
for fpath in [
|
|
|
|
"sysdata/limine.cfg",
|
|
|
|
"sysdata/background.bmp",
|
|
|
|
"target/test-programs/failure.hbf",
|
|
|
|
"target/test-programs/ecall.hbf",
|
|
|
|
"target/test-programs/main.hbf",
|
|
|
|
"target/test-programs/vfs_test.hbf",
|
|
|
|
"target/test-programs/limine_framebuffer_driver.hbf",
|
|
|
|
] {
|
|
|
|
let path = Path::new(fpath);
|
|
|
|
io::copy(
|
|
|
|
&mut File::open(path)?,
|
|
|
|
&mut fs
|
|
|
|
.root_dir()
|
|
|
|
.create_file(&path.file_name().unwrap().to_string_lossy())?,
|
|
|
|
)?;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-03-30 16:43:04 -05:00
|
|
|
fn build(release: bool, target: Target) -> Result<(), Error> {
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
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"]);
|
|
|
|
}
|
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"
|
|
|
|
}
|
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
|
|
|
|
2023-03-30 16:43:04 -05:00
|
|
|
fn run(release: bool, target: Target) -> Result<(), Error> {
|
|
|
|
let mut com = match target {
|
|
|
|
Target::X86_64 => Command::new("qemu-system-x86_64"),
|
|
|
|
Target::Riscv64Virt => Command::new("qemu-system-riscv64"),
|
2023-07-13 22:41:09 -05:00
|
|
|
Target::Aarch64 => Command::new("qemu-system-aarch64"),
|
2023-03-30 16:43:04 -05:00
|
|
|
};
|
2023-07-19 10:55:58 -05:00
|
|
|
let ovmf_path = fetch_ovmf(target);
|
|
|
|
|
|
|
|
match target {
|
|
|
|
Target::X86_64 => {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
com.args([
|
|
|
|
"-bios", &ovmf_path.change_context(Error::OvmfFetch)?,
|
|
|
|
"-drive", "file=target/disk.img,format=raw",
|
|
|
|
"-m", "4G",
|
|
|
|
"-smp", "cores=4",
|
2023-10-23 09:12:43 -05:00
|
|
|
"-cpu", "Broadwell-v4"
|
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",
|
|
|
|
"-cpu", "cortex-a72",
|
|
|
|
"-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 {
|
|
|
|
Target::X86_64 => (
|
|
|
|
"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)),
|
|
|
|
};
|
2023-07-19 10:55:58 -05:00
|
|
|
let mut bytes = reqwest::blocking::get(ovmf_url)
|
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
|
|
|
|
2023-07-19 10:55:58 -05:00
|
|
|
bytes
|
|
|
|
.copy_to(&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 {
|
|
|
|
#[display(fmt = "Failed to fetch OVMF package")]
|
|
|
|
Fetch,
|
2023-07-19 10:55:58 -05:00
|
|
|
#[display(fmt = "No OVMF package available")]
|
|
|
|
Empty,
|
2023-07-12 20:21:33 -05:00
|
|
|
#[display(fmt = "IO Error")]
|
|
|
|
Io,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Context for OvmfFetchError {}
|
|
|
|
|
2023-03-30 16:43:04 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
|
|
enum Target {
|
|
|
|
X86_64,
|
|
|
|
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
|
|
|
|
2023-07-12 20:21:33 -05:00
|
|
|
#[derive(Debug, Display)]
|
2023-03-30 16:43:04 -05:00
|
|
|
enum Error {
|
2023-07-12 20:21:33 -05:00
|
|
|
#[display(fmt = "Failed to build the kernel")]
|
2023-03-30 16:43:04 -05:00
|
|
|
Build,
|
2023-07-12 20:21:33 -05:00
|
|
|
#[display(fmt = "Missing or invalid subcommand (available: build, run)")]
|
2023-03-30 16:43:04 -05:00
|
|
|
InvalidSubCom,
|
2023-07-12 20:21:33 -05:00
|
|
|
#[display(fmt = "IO Error")]
|
2023-03-30 16:43:04 -05:00
|
|
|
Io,
|
2023-07-12 20:21:33 -05:00
|
|
|
#[display(fmt = "Failed to spawn a process")]
|
2023-03-30 16:43:04 -05:00
|
|
|
ProcessSpawn,
|
2023-07-12 20:21:33 -05:00
|
|
|
#[display(fmt = "Failed to fetch UEFI firmware")]
|
|
|
|
OvmfFetch,
|
2023-10-27 20:26:04 -05:00
|
|
|
#[display(fmt = "Failed to assemble Holey Bytes code")]
|
|
|
|
Assembler,
|
2023-07-12 20:21:33 -05:00
|
|
|
#[display(fmt = "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
|
|
|
|
|
|
|
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
|
|
|
}
|