|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +import tempfile |
| 8 | + |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | + |
| 12 | +def run(*cmd, capture=False, check=True, env=None): |
| 13 | + """Print and run a command, optionally capturing the output.""" |
| 14 | + cmd = [str(p) for p in cmd] |
| 15 | + print(' '.join(cmd)) |
| 16 | + return subprocess.run(cmd, |
| 17 | + capture_output=capture, |
| 18 | + check=check, |
| 19 | + env=env, |
| 20 | + text=True) |
| 21 | + |
| 22 | + |
| 23 | +def build_and_run(tmp_dir): |
| 24 | + host_artifacts = Path('/checkout/obj/build/x86_64-unknown-linux-gnu') |
| 25 | + stage0 = host_artifacts / 'stage0/bin' |
| 26 | + stage2 = host_artifacts / 'stage2/bin' |
| 27 | + |
| 28 | + env = dict(os.environ) |
| 29 | + env['PATH'] = '{}:{}:{}'.format(stage2, stage0, env['PATH']) |
| 30 | + |
| 31 | + # Copy the test create into `tmp_dir`. |
| 32 | + test_crate = Path(tmp_dir) / 'uefi_qemu_test' |
| 33 | + shutil.copytree('/uefi_qemu_test', test_crate) |
| 34 | + |
| 35 | + # Build the UEFI executable. |
| 36 | + target = 'x86_64-unknown-uefi' |
| 37 | + run('cargo', |
| 38 | + 'build', |
| 39 | + '--manifest-path', |
| 40 | + test_crate / 'Cargo.toml', |
| 41 | + '--target', |
| 42 | + target, |
| 43 | + env=env) |
| 44 | + |
| 45 | + # Create a mock EFI System Partition in a subdirectory. |
| 46 | + esp = test_crate / 'esp' |
| 47 | + boot = esp / 'efi/boot' |
| 48 | + os.makedirs(boot, exist_ok=True) |
| 49 | + |
| 50 | + # Copy the executable into the ESP. |
| 51 | + src_exe_path = test_crate / 'target' / target / 'debug/uefi_qemu_test.efi' |
| 52 | + shutil.copy(src_exe_path, boot / 'bootx64.efi') |
| 53 | + |
| 54 | + # Run the executable in QEMU and capture the output. |
| 55 | + qemu = 'qemu-system-x86_64' |
| 56 | + ovmf_dir = Path('/usr/share/OVMF') |
| 57 | + ovmf_code = ovmf_dir / 'OVMF_CODE.fd' |
| 58 | + ovmf_vars = ovmf_dir / 'OVMF_VARS.fd' |
| 59 | + output = run(qemu, |
| 60 | + '-display', |
| 61 | + 'none', |
| 62 | + '-serial', |
| 63 | + 'stdio', |
| 64 | + '-drive', |
| 65 | + f'if=pflash,format=raw,readonly=on,file={ovmf_code}', |
| 66 | + '-drive', |
| 67 | + f'if=pflash,format=raw,readonly=on,file={ovmf_vars}', |
| 68 | + '-drive', |
| 69 | + f'format=raw,file=fat:rw:{esp}', |
| 70 | + capture=True, |
| 71 | + # Ubuntu 20.04 (which is what the Dockerfile currently |
| 72 | + # uses) provides QEMU 4.2.1, which segfaults on |
| 73 | + # shutdown under some circumstances. That has been |
| 74 | + # fixed in newer versions of QEMU, but for now just |
| 75 | + # don't check the exit status. |
| 76 | + check=False).stdout |
| 77 | + |
| 78 | + if 'Hello World!' in output: |
| 79 | + print('VM produced expected output') |
| 80 | + else: |
| 81 | + print('unexpected VM output:') |
| 82 | + print('---start---') |
| 83 | + print(output) |
| 84 | + print('---end---') |
| 85 | + sys.exit(1) |
| 86 | + |
| 87 | + |
| 88 | +def main(): |
| 89 | + # Create a temporary directory so that we have a writeable |
| 90 | + # workspace. |
| 91 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 92 | + build_and_run(tmp_dir) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + main() |
0 commit comments