ableos/repbuild/src/main.rs

492 lines
19 KiB
Rust
Raw Normal View History

2022-08-03 02:11:51 -05:00
/*
2022-08-12 08:40:23 -05:00
* Copyright (c) 2022, Umut İnan Erdoğan <umutinanerdogan@pm.me>
* Copyright (c) 2022, able <abl3theabove@gmail.com>
*
* SPDX-License-Identifier: MPL-2.0
*/
2022-08-03 02:11:51 -05:00
use colored::*;
use std::{
fs::{self, File},
os::fd::AsRawFd,
process::Command,
};
use udisks::{filesystem::UnMountOptions, manager::LoopSetupOptions};
2022-08-03 02:11:51 -05:00
struct Options {
2022-08-07 07:35:55 -05:00
pub subcommand: Subcommand,
pub arguments: Vec<String>,
2022-08-03 02:11:51 -05:00
}
enum Subcommand {
BuildImage,
2022-08-07 07:35:55 -05:00
Doc,
Help,
Run,
Empty,
/// Run all tests for all architectures
Test,
Unknown(String),
2022-08-03 02:11:51 -05:00
}
impl Subcommand {
2022-08-07 07:35:55 -05:00
fn from_str<S: AsRef<str>>(str: S) -> Subcommand {
match str.as_ref() {
"build-image" => Subcommand::BuildImage,
2022-08-07 07:35:55 -05:00
"doc" => Subcommand::Doc,
"help" => Subcommand::Help,
"run" | "r" => Subcommand::Run,
"test" | "t" => Subcommand::Test,
"" => Subcommand::Empty,
unknown => Subcommand::Unknown(unknown.to_string()),
}
}
2022-08-03 02:11:51 -05:00
}
enum MachineType {
2022-08-07 07:35:55 -05:00
X86_64,
RiscV64,
AArch64,
Unknown(String),
2022-08-03 02:11:51 -05:00
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
2022-08-07 07:35:55 -05:00
let options = options();
match options.subcommand {
Subcommand::BuildImage => {
let machine_text = options.arguments.get(0).cloned().unwrap_or_default();
match machine(machine_text) {
MachineType::X86_64 => {
// Cleanup
// NOTE: we are not unwrapping these, as we don't want this to fail if they
// don't exist yet, probably not the best idea tho.
// FIXME: figure out a better way to ignore errors about these not existing
#[allow(unused_must_use)]
{
fs::remove_dir_all("./limine");
fs::remove_dir_all("./disk");
fs::remove_file("./disk.img");
}
// Build ableOS in release mode
Command::new("cargo")
.args(["build", "--release"])
.current_dir(fs::canonicalize("./ableos").unwrap())
.status()
.unwrap();
// Create disk directory
fs::create_dir("./disk").unwrap();
// Clone limine 3.X binaries
Command::new("git")
.arg("clone")
.arg("https://github.com/limine-bootloader/limine.git")
.arg("--branch=v3.0-branch-binary")
.arg("--depth=1")
.status()
.unwrap();
println!("{}", "Building limine".bold());
Command::new("make")
.args(["-C", "limine"])
.status()
.unwrap();
println!("{}", "Allocating new disk image".bold());
Command::new("fallocate")
.args(["-l", "256M", "disk.img"])
.status()
.unwrap();
println!("{}", "Partitioning disk image".bold());
let dbus_conn = zbus::blocking::Connection::system()?;
2022-11-24 03:35:55 -06:00
// Setup loop device
let disk_img = File::options().read(true).write(true).open("disk.img")?;
let loopdev = udisks::manager::UDisks2ManagerProxyBlocking::new(&dbus_conn)?
.loop_setup(
disk_img.as_raw_fd().into(),
LoopSetupOptions {
no_user_interaction: true,
offset: 0,
size: 0,
readonly: false,
no_part_scan: false,
},
)?;
// Create MBR
udisks::block::BlockProxyBlocking::builder(&dbus_conn)
.path(&loopdev)?
.build()?
.format("dos", Default::default())?;
// Create and format partition
let filesystem =
udisks::partition::PartitionTableProxyBlocking::builder(&dbus_conn)
.destination("org.freedesktop.UDisks2")?
.path(&loopdev)?
.build()?
.create_partition_and_format(
0,
0,
"",
"",
Default::default(),
2022-11-24 03:35:55 -06:00
"ext2",
[("take-ownership", true.into())].into_iter().collect(),
)?;
let fsproxy = udisks::filesystem::FilesystemProxyBlocking::builder(&dbus_conn)
.path(&filesystem)?
.build()?;
// Obtain mountpoint
let mountpoint = loop {
if let Some(m) = fsproxy.mount_points()?.get(0) {
break m.to_string();
}
};
// copy ./base/* over to ./disk
Command::new("sh")
.arg("-c")
.arg(format!("cp -r ./base/* {mountpoint}"))
.status()?;
// copy ./limine/limine.sys over to ./disk/boot
Command::new("cp")
.args(["./limine/limine.sys", &format!("{mountpoint}/boot")])
.status()?;
// copy the kernel over to ./disk/boot/kernel
Command::new("cp")
.arg("./target/x86_64-ableos/release/ableos")
.arg(&format!("{mountpoint}/boot/kernel"))
.status()?;
// Unmount the filesystem (and the rest of things will follow)
fsproxy.unmount(UnMountOptions {
no_user_interaction: true,
force: false,
})?;
println!("{}", "Deploying limine".bold());
Command::new("./limine/limine-deploy")
.arg("./disk.img")
.status()
.unwrap();
}
MachineType::Unknown(unknown) => {
eprintln!(
"{}: unknown machine type `{}`",
"error".red().bold(),
unknown.bold(),
);
eprintln!("expected one of x86_64, riscv64 or aarch64");
}
_ => {
eprintln!(
"{}: build-image not implemented for this machine type",
"error".red().bold(),
);
}
}
}
2022-08-07 07:35:55 -05:00
Subcommand::Test => {
Command::new("cargo")
.args(["test", "--target=json_targets/x86_64-ableos.json"])
.current_dir(fs::canonicalize("./ableos").unwrap())
.status()
.unwrap();
// panic!("Test Infrastructure missing");
}
Subcommand::Doc => {
let machine_text = options.arguments.get(0).cloned().unwrap_or_default();
match machine(machine_text) {
MachineType::X86_64 => {
Command::new("cargo")
.args(["doc", "--open"])
.current_dir(fs::canonicalize("./ableos").unwrap())
.status()
.unwrap();
}
MachineType::RiscV64 => {
Command::new("cargo")
.args(["doc", "--open", "--target=riscv64gc-unknown-none-elf"])
.current_dir(fs::canonicalize("./ableos").unwrap())
.status()
.unwrap();
}
MachineType::AArch64 => {
Command::new("cargo")
.args(["doc", "--open", "--target=json_targets/aarch64-ableos.json"])
.current_dir(fs::canonicalize("./ableos").unwrap())
.status()
.unwrap();
}
MachineType::Unknown(unknown) => {
eprintln!(
"{}: unknown machine type `{}`",
"error".red().bold(),
unknown.bold(),
);
eprintln!("expected one of x86_64, riscv64 or aarch64");
}
2022-08-03 02:11:51 -05:00
}
2022-08-07 07:35:55 -05:00
}
Subcommand::Help => help(),
Subcommand::Run => {
let machine_text = options.arguments.get(0).cloned().unwrap_or_default();
let debug = options.arguments.get(1).cloned().unwrap_or_default();
let debug = matches!(debug.as_str(), "--debug" | "--dbg" | "-d");
match machine(machine_text) {
MachineType::X86_64 if debug => {
// Build ableOS
2022-08-07 07:35:55 -05:00
Command::new("cargo")
.arg("build")
2022-08-07 07:35:55 -05:00
.current_dir(fs::canonicalize("./ableos").unwrap())
.status()
.unwrap();
// Setup loopback device for disk.img, with partitions
// FIXME: don't do ths if running without changes
Command::new("sudo")
.arg("losetup")
.args(["-P", "/dev/loop0", "disk.img"])
.status()
.unwrap();
// mount the root fs to ./disk
Command::new("sudo")
.arg("mount")
.args(["/dev/loop0p1", "./disk"])
.status()
.unwrap();
// copy the kernel over to ./disk/boot/kernel
Command::new("sudo")
.arg("cp")
.arg("./target/x86_64-ableos/debug/ableos")
.arg("./disk/boot/kernel")
.status()
.unwrap();
// unmount the root fs
Command::new("sudo")
.arg("umount")
.arg("./disk")
.status()
.unwrap();
// remove loopback device
Command::new("sudo")
.arg("losetup")
.args(["-d", "/dev/loop0"])
.status()
.unwrap();
// run qemu with "-S", "-gdb", "tcp:9000"
Command::new("qemu-system-x86_64")
.args(["-device", "piix4-ide,id=ide"])
.arg("-drive")
.arg("file=./disk.img,format=raw,if=none,id=disk")
.args(["-device", "ide-hd,drive=disk,bus=ide.0"])
// .arg("--nodefaults")
.args(["-cpu", "Broadwell-v3"])
.args(["-m", "4G"])
.args(["-serial", "stdio"])
.args(["-smp", "cores=2"])
// .args(["-soundhw", "pcspk"])
// .args(["-device", "VGA"])
// .args(["-device", "virtio-gpu-pci"])
.args(["-device", "vmware-svga"])
.args(["-device", "sb16"])
// .args(["-machine", "pcspk-audiodev=0"])
// .args(["-qmp", "unix:../qmp-sock,server,nowait"])
.args(["-S", "-gdb", "tcp:9000"])
.status()
.unwrap();
2022-08-07 07:35:55 -05:00
}
MachineType::X86_64 => {
// Build ableOS
2022-08-07 07:35:55 -05:00
Command::new("cargo")
.args(["build", "--release"])
2022-08-07 07:35:55 -05:00
.current_dir(fs::canonicalize("./ableos").unwrap())
.status()
.unwrap();
// Setup loopback device for disk.img, with partitions
// FIXME: don't do ths if running without changes
Command::new("sudo")
.arg("losetup")
.args(["-P", "/dev/loop0", "disk.img"])
.status()
.unwrap();
// mount the root fs to ./disk
Command::new("sudo")
.arg("mount")
.args(["/dev/loop0p1", "./disk"])
.status()
.unwrap();
// copy the kernel over to ./disk/boot/kernel
Command::new("sudo")
.arg("cp")
.arg("./target/x86_64-ableos/release/ableos")
.arg("./disk/boot/kernel")
.status()
.unwrap();
// unmount the root fs
Command::new("sudo")
.arg("umount")
.arg("./disk")
.status()
.unwrap();
// remove loopback device
Command::new("sudo")
.arg("losetup")
.args(["-d", "/dev/loop0"])
.status()
.unwrap();
// run qemu
Command::new("qemu-system-x86_64")
.args(["-device", "piix4-ide,id=ide"])
.arg("-drive")
.arg("file=./disk.img,format=raw,if=none,id=disk")
.args(["-device", "ide-hd,drive=disk,bus=ide.0"])
// .arg("--nodefaults")
.args(["-cpu", "Broadwell-v3"])
.args(["-m", "4G"])
.args(["-serial", "stdio"])
.args(["-smp", "cores=2"])
// .args(["-soundhw", "pcspk"])
// .args(["-device", "VGA"])
// .args(["-device", "virtio-gpu-pci"])
.args(["-device", "vmware-svga"])
.args(["-device", "sb16"])
// .args(["-machine", "pcspk-audiodev=0"])
// .args(["-qmp", "unix:../qmp-sock,server,nowait"])
.status()
.unwrap();
2022-08-07 07:35:55 -05:00
}
MachineType::RiscV64 if debug => {
eprintln!(
"{}: debug is not implemented for riscv64",
"error".red().bold()
);
}
MachineType::RiscV64 => {
Command::new("cargo")
.args(["build", "--release", "--target=riscv64gc-unknown-none-elf"])
.current_dir(fs::canonicalize("./ableos").unwrap())
.status()
.unwrap();
Command::new("qemu-system-riscv64")
.args(["-machine", "virt"])
.args(["-cpu", "rv64"])
.args(["-smp", "8"])
.args(["-m", "128M"])
.arg("-bios")
.arg("src/arch/riscv/firmwear/opensbi-riscv64-generic-fw_jump.bin")
.arg("-kernel")
.arg("target/riscv64gc-unknown-none-elf/release/ableos")
.current_dir(fs::canonicalize("./ableos").unwrap())
.status()
.unwrap();
}
MachineType::AArch64 if debug => {
eprintln!(
"{}: debug is not implemented for aarch64",
"error".red().bold()
);
}
MachineType::AArch64 => {
Command::new("cargo")
.args([
"build",
"--release",
"--target=json_targets/aarch64-ableos.json",
])
.current_dir(fs::canonicalize("./ableos").unwrap())
.status()
.unwrap();
Command::new("qemu-system-aarch64")
.args(["-machine", "virt"])
.args(["-m", "1024M"])
.args(["-cpu", "cortex-a53"])
.args(["-kernel", "target/aarch64-ableos/release/ableos"])
.args(["-device", "virtio-keyboard"])
.current_dir(fs::canonicalize("./ableos").unwrap())
.status()
.unwrap();
}
MachineType::Unknown(unknown) => {
eprintln!(
"{}: unknown machine type `{}`",
"error".red().bold(),
unknown.bold(),
);
eprintln!("expected one of x86_64, riscv64 or aarch64");
}
2022-08-03 02:11:51 -05:00
}
2022-08-07 07:35:55 -05:00
}
Subcommand::Empty => {
eprintln!("{}: no subcommand passed", "error".red().bold());
help();
}
Subcommand::Unknown(unknown) => {
eprintln!(
"{}: unknown subcommand `{}`",
"error".red().bold(),
unknown.bold()
);
help();
}
}
Ok(())
2022-08-03 02:11:51 -05:00
}
fn options() -> Options {
2022-08-07 07:35:55 -05:00
let subcommand = std::env::args().nth(1).unwrap_or_default();
let arguments = std::env::args().skip(2).collect();
2022-08-03 02:11:51 -05:00
2022-08-07 07:35:55 -05:00
Options {
subcommand: Subcommand::from_str(subcommand),
arguments,
}
2022-08-03 02:11:51 -05:00
}
fn machine<S: AsRef<str>>(text: S) -> MachineType {
2022-08-07 07:35:55 -05:00
match text.as_ref() {
"x86" | "x86_64" => MachineType::X86_64,
"riscv" | "riscv64" => MachineType::RiscV64,
"arm" | "arm64" | "aarch64" => MachineType::AArch64,
"" => {
eprintln!(
"{}: no machine type passed, defaulting to x86_64",
"warning".yellow().bold()
);
MachineType::X86_64
}
unknown => MachineType::Unknown(unknown.to_string()),
}
2022-08-03 02:11:51 -05:00
}
fn help() {
2022-08-07 07:35:55 -05:00
todo!("`help`")
2022-08-03 02:11:51 -05:00
}