|
| 1 | +//! This test builds and runs a basic UEFI application on QEMU for various targets. |
| 2 | +//! |
| 3 | +//! You must have the relevant OVMF or AAVMF firmware installed for this to work. |
| 4 | +//! |
| 5 | +//! Requires: qemu-system-x86_64, qemu-system-aarch64 |
| 6 | +//! OVMF/AAVMF firmware |
| 7 | +//! |
| 8 | +//! Note: test assumes `/uefi_qemu_test` exists and is a self-contained crate. |
| 9 | +
|
| 10 | +//@ only-uefi |
| 11 | + |
| 12 | +use std::path::Path; |
| 13 | + |
| 14 | +use run_make_support::{cargo, cmd, path, target}; |
| 15 | + |
| 16 | +fn main() { |
| 17 | + let tmp = std::env::temp_dir(); |
| 18 | + build_and_run(&target(), &tmp); |
| 19 | +} |
| 20 | + |
| 21 | +fn build_and_run(target: &str, tmp: &Path) { |
| 22 | + let (boot_filename, ovmf_dir, ovmf_code_name, ovmf_vars_name, qemu, machine, cpu) = match target |
| 23 | + { |
| 24 | + "aarch64-unknown-uefi" => ( |
| 25 | + "bootaa64.efi", |
| 26 | + Path::new("/usr/share/AAVMF"), |
| 27 | + "AAVMF_CODE.fd", |
| 28 | + "AAVMF_VARS.fd", |
| 29 | + "qemu-system-aarch64", |
| 30 | + "virt", |
| 31 | + "cortex-a72", |
| 32 | + ), |
| 33 | + "i686-unknown-uefi" => ( |
| 34 | + "bootia32.efi", |
| 35 | + Path::new("/usr/share/OVMF"), |
| 36 | + "OVMF32_CODE_4M.secboot.fd", |
| 37 | + "OVMF32_VARS_4M.fd", |
| 38 | + "qemu-system-x86_64", |
| 39 | + "q35", |
| 40 | + "qemu64", |
| 41 | + ), |
| 42 | + "x86_64-unknown-uefi" => ( |
| 43 | + "bootx64.efi", |
| 44 | + Path::new("/usr/share/OVMF"), |
| 45 | + "OVMF_CODE_4M.fd", |
| 46 | + "OVMF_VARS_4M.fd", |
| 47 | + "qemu-system-x86_64", |
| 48 | + "q35", |
| 49 | + "qemu64", |
| 50 | + ), |
| 51 | + _ => panic!("unsupported target {target}"), |
| 52 | + }; |
| 53 | + |
| 54 | + let test_crate = tmp.join("uefi_qemu_test"); |
| 55 | + copy_tree(path("uefi_qemu_test"), &test_crate); |
| 56 | + |
| 57 | + // Build |
| 58 | + cargo().args(&["build", "--target", target]).current_dir(&test_crate).run(); |
| 59 | + |
| 60 | + // Prepare ESP |
| 61 | + let esp = test_crate.join("esp"); |
| 62 | + let boot = esp.join("efi/boot"); |
| 63 | + std::fs::create_dir_all(&boot).unwrap(); |
| 64 | + |
| 65 | + let src_efi = test_crate.join("target").join(target).join("debug/uefi_qemu_test.efi"); |
| 66 | + let dst_efi = boot.join(boot_filename); |
| 67 | + std::fs::copy(&src_efi, &dst_efi).unwrap(); |
| 68 | + |
| 69 | + // Copy OVMF files |
| 70 | + let code = ovmf_dir.join(ovmf_code_name); |
| 71 | + let vars_src = ovmf_dir.join(ovmf_vars_name); |
| 72 | + let vars_dst = tmp.join("vars.fd"); |
| 73 | + std::fs::copy(&vars_src, &vars_dst).unwrap(); |
| 74 | + |
| 75 | + // Run QEMU |
| 76 | + let output = cmd(qemu) |
| 77 | + .args(["-machine", machine]) |
| 78 | + .args(["-cpu", cpu]) |
| 79 | + .args(["-display", "none"]) |
| 80 | + .args(["-serial", "stdio"]) |
| 81 | + .args(["-drive", &format!("if=pflash,format=raw,readonly=on,file={}", code.display())]) |
| 82 | + .args(["-drive", &format!("if=pflash,format=raw,readonly=off,file={}", vars_dst.display())]) |
| 83 | + .args(["-drive", &format!("format=raw,file=fat:rw:{}", esp.display())]) |
| 84 | + .run() |
| 85 | + .stdout_utf8(); |
| 86 | + |
| 87 | + assert!(output.contains("Hello World!"), "invalid output for {target}:\n{output}"); |
| 88 | +} |
| 89 | + |
| 90 | +fn copy_tree(src: impl AsRef<Path>, dst: impl AsRef<Path>) { |
| 91 | + fn inner(src: &Path, dst: &Path) { |
| 92 | + if !dst.exists() { |
| 93 | + std::fs::create_dir_all(dst).unwrap(); |
| 94 | + } |
| 95 | + |
| 96 | + for entry in std::fs::read_dir(src).unwrap() { |
| 97 | + let entry = entry.unwrap(); |
| 98 | + let file_type = entry.file_type().unwrap(); |
| 99 | + let src_path = entry.path(); |
| 100 | + let dst_path = dst.join(entry.file_name()); |
| 101 | + |
| 102 | + if file_type.is_dir() { |
| 103 | + inner(&src_path, &dst_path); |
| 104 | + } else if file_type.is_file() { |
| 105 | + std::fs::copy(&src_path, &dst_path).unwrap(); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + inner(src.as_ref(), dst.as_ref()); |
| 111 | +} |
0 commit comments