summaryrefslogtreecommitdiff
path: root/x
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2026-09-05 19:01:32 +0800
committerlittleblack111 <littleblack11111@gmail.com>2026-09-05 19:01:32 +0800
commit365b581ca5c37305f9583cf08d0c25c4426f47e2 (patch)
tree4a77be797bc192a5919728a7337937407075c119 /x
parentb4d9a57f193c64bca188a76e4f711cc168da9737 (diff)
migrate: limine -> bootloader_api
Diffstat (limited to 'x')
-rw-r--r--x/.cargo/config.toml2
-rw-r--r--x/Cargo.toml11
-rw-r--r--x/build.rs25
-rw-r--r--x/src/main.rs62
4 files changed, 100 insertions, 0 deletions
diff --git a/x/.cargo/config.toml b/x/.cargo/config.toml
new file mode 100644
index 0000000..dfa84e8
--- /dev/null
+++ b/x/.cargo/config.toml
@@ -0,0 +1,2 @@
+[unstable]
+bindeps = true
diff --git a/x/Cargo.toml b/x/Cargo.toml
new file mode 100644
index 0000000..0dc6279
--- /dev/null
+++ b/x/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "x"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
+ovmf-prebuilt = "0.2.4"
+
+[build-dependencies]
+kernel = { path = "../kernel", artifact = "bin", target = "x86_64-unknown-none" }
+bootloader = { git = "https://github.com/rust-osdev/bootloader" }
diff --git a/x/build.rs b/x/build.rs
new file mode 100644
index 0000000..37200d3
--- /dev/null
+++ b/x/build.rs
@@ -0,0 +1,25 @@
+use std::path::PathBuf;
+
+fn main() {
+ // set by cargo, build scripts should use this directory for output files
+ let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
+ // set by cargo's artifact dependency feature, see
+ // https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#artifact-dependencies
+ let kernel = PathBuf::from(std::env::var_os("CARGO_BIN_FILE_KERNEL_kernel").unwrap());
+
+ // create an UEFI disk image (optional)
+ let uefi_path = out_dir.join("uefi.img");
+ bootloader::UefiBoot::new(&kernel)
+ .create_disk_image(&uefi_path)
+ .unwrap();
+
+ // create a BIOS disk image
+ let bios_path = out_dir.join("bios.img");
+ bootloader::BiosBoot::new(&kernel)
+ .create_disk_image(&bios_path)
+ .unwrap();
+
+ // pass the disk image paths as env variables to the
+ println!("cargo:rustc-env=UEFI_PATH={}", uefi_path.display());
+ println!("cargo:rustc-env=BIOS_PATH={}", bios_path.display());
+}
diff --git a/x/src/main.rs b/x/src/main.rs
new file mode 100644
index 0000000..f2eabf9
--- /dev/null
+++ b/x/src/main.rs
@@ -0,0 +1,62 @@
+use ovmf_prebuilt::{Arch, FileType, Prebuilt, Source};
+use std::{
+ env,
+ process::{Command, exit},
+};
+
+fn main() {
+ // read env variables that were set in build script
+ let uefi_path = env!("UEFI_PATH");
+ let bios_path = env!("BIOS_PATH");
+
+ // parse mode from CLI
+ let args: Vec<String> = env::args().collect();
+ let prog = &args[0];
+
+ // choose whether to start the UEFI or BIOS image
+ let uefi = match args.get(1).map(|s| s.to_lowercase()) {
+ Some(ref s) if s == "uefi" => true,
+ Some(ref s) if s == "bios" => false,
+ Some(ref s) if s == "-h" || s == "--help" => {
+ println!("Usage: {prog} [uefi|bios]");
+ println!(" uefi - boot using OVMF (UEFI)");
+ println!(" bios - boot using legacy BIOS");
+ exit(0);
+ }
+ _ => true,
+ };
+
+ let mut cmd = Command::new("qemu-system-x86_64");
+ // print serial output to the shell
+ cmd.arg("-serial").arg("mon:stdio");
+ // don't display video output
+ cmd.arg("-display").arg("none");
+ // enable the guest to exit qemu
+ cmd.arg("-device").arg("isa-debug-exit,iobase=0xf4,iosize=0x04");
+
+ if uefi {
+ let prebuilt =
+ Prebuilt::fetch(Source::LATEST, "target/ovmf").expect("failed to update prebuilt");
+
+ let code = prebuilt.get_file(Arch::X64, FileType::Code);
+ let vars = prebuilt.get_file(Arch::X64, FileType::Vars);
+
+ cmd.arg("-drive").arg(format!("format=raw,file={uefi_path}"));
+ cmd.arg("-drive")
+ .arg(format!("if=pflash,format=raw,unit=0,file={},readonly=on", code.display()));
+ // copy vars and enable rw instead of snapshot if you want to store data
+ // (e.g. enroll secure boot keys)
+ cmd.arg("-drive")
+ .arg(format!("if=pflash,format=raw,unit=1,file={},snapshot=on", vars.display()));
+ } else {
+ cmd.arg("-drive").arg(format!("format=raw,file={bios_path}"));
+ }
+
+ let mut child = cmd.spawn().expect("failed to start qemu-system-x86_64");
+ let status = child.wait().expect("failed to wait on qemu");
+ match status.code().unwrap_or(1) {
+ 0x10 => 0, // success
+ 0x11 => 1, // failure
+ _ => 2, // unknown fault
+ };
+}