|
| 1 | +use std::{ |
| 2 | + path::{Path, PathBuf}, |
| 3 | + process::Command, |
| 4 | +}; |
| 5 | + |
| 6 | +const RUN_ARGS: &[&str] = &["--no-reboot", "-s"]; |
| 7 | + |
| 8 | +fn main() { |
| 9 | + let mut args = std::env::args().skip(1); // skip executable name |
| 10 | + |
| 11 | + let kernel_binary_path = { |
| 12 | + let path = PathBuf::from(args.next().unwrap()); |
| 13 | + path.canonicalize().unwrap() |
| 14 | + }; |
| 15 | + let no_boot = if let Some(arg) = args.next() { |
| 16 | + match arg.as_str() { |
| 17 | + "--no-run" => true, |
| 18 | + other => panic!("unexpected argument `{}`", other), |
| 19 | + } |
| 20 | + } else { |
| 21 | + false |
| 22 | + }; |
| 23 | + |
| 24 | + let bios = create_disk_images(&kernel_binary_path); |
| 25 | + |
| 26 | + if no_boot { |
| 27 | + println!("Created disk image at `{}`", bios.display()); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + let mut run_cmd = Command::new("qemu-system-x86_64"); |
| 32 | + run_cmd |
| 33 | + .arg("-drive") |
| 34 | + .arg(format!("format=raw,file={}", bios.display())); |
| 35 | + run_cmd.args(RUN_ARGS); |
| 36 | + |
| 37 | + let exit_status = run_cmd.status().unwrap(); |
| 38 | + if !exit_status.success() { |
| 39 | + std::process::exit(exit_status.code().unwrap_or(1)); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +pub fn create_disk_images(kernel_binary_path: &Path) -> PathBuf { |
| 44 | + let bootloader_manifest_path = bootloader_locator::locate_bootloader("bootloader").unwrap(); |
| 45 | + let kernel_manifest_path = locate_cargo_manifest::locate_manifest().unwrap(); |
| 46 | + |
| 47 | + let mut build_cmd = Command::new(env!("CARGO")); |
| 48 | + build_cmd.current_dir(bootloader_manifest_path.parent().unwrap()); |
| 49 | + build_cmd.arg("builder"); |
| 50 | + build_cmd |
| 51 | + .arg("--kernel-manifest") |
| 52 | + .arg(&kernel_manifest_path); |
| 53 | + build_cmd.arg("--kernel-binary").arg(&kernel_binary_path); |
| 54 | + build_cmd |
| 55 | + .arg("--target-dir") |
| 56 | + .arg(kernel_manifest_path.parent().unwrap().join("target")); |
| 57 | + build_cmd |
| 58 | + .arg("--out-dir") |
| 59 | + .arg(kernel_binary_path.parent().unwrap()); |
| 60 | + build_cmd.arg("--quiet"); |
| 61 | + |
| 62 | + if !build_cmd.status().unwrap().success() { |
| 63 | + panic!("build failed"); |
| 64 | + } |
| 65 | + |
| 66 | + let kernel_binary_name = kernel_binary_path.file_name().unwrap().to_str().unwrap(); |
| 67 | + let disk_image = kernel_binary_path |
| 68 | + .parent() |
| 69 | + .unwrap() |
| 70 | + .join(format!("boot-bios-{}.img", kernel_binary_name)); |
| 71 | + if !disk_image.exists() { |
| 72 | + panic!( |
| 73 | + "Disk image does not exist at {} after bootloader build", |
| 74 | + disk_image.display() |
| 75 | + ); |
| 76 | + } |
| 77 | + disk_image |
| 78 | +} |
0 commit comments