diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2026-09-05 19:01:32 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2026-09-05 19:01:32 +0800 |
| commit | 365b581ca5c37305f9583cf08d0c25c4426f47e2 (patch) | |
| tree | 4a77be797bc192a5919728a7337937407075c119 /x/src/main.rs | |
| parent | b4d9a57f193c64bca188a76e4f711cc168da9737 (diff) | |
migrate: limine -> bootloader_api
Diffstat (limited to 'x/src/main.rs')
| -rw-r--r-- | x/src/main.rs | 62 |
1 files changed, 62 insertions, 0 deletions
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 + }; +} |
