Skip to content

Commit 32cabdd

Browse files
committed
Setup test framework for loader and update OVMF.
1 parent e2cadea commit 32cabdd

File tree

7 files changed

+88
-6
lines changed

7 files changed

+88
-6
lines changed

Makefile

+33-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ OVMF=ovmf/bios64.bin
66
QEMU=qemu-system-x86_64
77
OSNAME=${shell uname -s}
88

9+
QEMU_ARGS_LOADER_TEST=\
10+
-device qemu-xhci -device usb-mouse \
11+
-bios $(OVMF) \
12+
-machine q35,nvdimm -cpu qemu64 -smp 4 \
13+
-m 2G,slots=2,maxmem=4G \
14+
-drive format=raw,file=fat:rw:mnt -net none \
15+
-rtc base=localtime \
16+
-device isa-debug-exit,iobase=0xf4,iosize=0x01 \
17+
-chardev stdio,id=char0,mux=on \
18+
-monitor none \
19+
-serial chardev:char0 \
20+
-serial chardev:char0 \
21+
-nographic
22+
923
QEMU_ARGS_COMMON=\
1024
-device qemu-xhci -device usb-mouse \
1125
-bios $(OVMF) \
@@ -15,7 +29,6 @@ QEMU_ARGS_COMMON=\
1529
-m 2G,slots=2,maxmem=4G \
1630
-drive format=raw,file=fat:rw:mnt -net none \
1731
-rtc base=localtime \
18-
-d cpu_reset \
1932
-serial tcp::1234,server,nowait \
2033
-serial tcp::1235,server,nowait
2134

@@ -98,6 +111,24 @@ files : src/BOOTX64.EFI src/LIUMOS.ELF .FORCE
98111
mkdir -p mnt/EFI/EFI/
99112
echo 'FS0:\\EFI\\BOOT\\BOOTX64.EFI' > mnt/startup.nsh
100113

114+
.PHONY : internal_run_loader_test
115+
116+
internal_run_loader_test :
117+
@echo Using ${LOADER_TEST_EFI} as a loader...
118+
mkdir -p mnt/
119+
-rm -rf mnt/*
120+
mkdir -p mnt/EFI/BOOT
121+
cp ${LOADER_TEST_EFI} mnt/EFI/BOOT/BOOTX64.EFI
122+
$(QEMU) $(QEMU_ARGS_LOADER_TEST) ; \
123+
RETCODE=$$? ; \
124+
if [ $$RETCODE -eq 3 ]; then \
125+
echo "\nPASS" ; \
126+
exit 0 ; \
127+
else \
128+
echo "\nFAIL: QEMU returned $$RETCODE" ; \
129+
exit 1 ; \
130+
fi
131+
101132
run_nopmem : files .FORCE
102133
$(QEMU) $(QEMU_ARGS)
103134

@@ -189,6 +220,7 @@ test :
189220
make spellcheck
190221
make -C src test
191222
make -C app/liumlib test
223+
make -C loader test
192224

193225
e2etest_root :
194226
make -C e2etest test

loader/.cargo/config.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[unstable]
2+
build-std = ["core", "compiler_builtins"]
3+
build-std-features = ["compiler-builtins-mem"]
4+
5+
[build]
6+
target = "x86_64-none-efi.json"
7+
8+
[target.'cfg(target_os = "uefi")']
9+
runner = "scripts/test_runner.sh"

loader/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,5 @@ edition = "2018"
77
[dependencies]
88

99
[profile.dev]
10-
panic = "abort"
1110

1211
[profile.release]
13-
panic = "abort"

loader/Makefile

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ include ../common.mk
55
.PHONY: build clean
66

77
build :
8-
cargo build -vv --target=x86_64-none-efi.json --release \
9-
-Z build-std=core,alloc \
10-
-Z build-std-features=compiler-builtins-mem
8+
cargo build -vv --release
9+
1110
clean:
1211
-rm -r target
1312

@@ -16,3 +15,6 @@ install:
1615

1716
check:
1817
cargo clippy
18+
19+
test:
20+
cargo test

loader/scripts/test_runner.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash -xe
2+
LOADER_TEST_EFI="$1"
3+
cd `dirname $0`
4+
cd ../..
5+
LOADER_TEST_EFI="${LOADER_TEST_EFI}" make internal_run_loader_test

loader/src/main.rs

+36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#![no_std]
22
#![no_main]
33
#![feature(asm)]
4+
#![feature(custom_test_frameworks)]
5+
#![test_runner(crate::test_runner)]
6+
#![reexport_test_harness_main = "test_main"]
47

58
use core::convert::TryInto;
69
use core::fmt;
@@ -73,8 +76,23 @@ impl fmt::Write for Writer {
7376
}
7477
}
7578

79+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
80+
#[repr(u32)]
81+
pub enum QemuExitCode {
82+
Success = 0x1, // QEMU will exit with status 3
83+
Failed = 0x2, // QEMU will exit with status 5
84+
}
85+
86+
pub fn exit_qemu(exit_code: QemuExitCode) {
87+
// https://github.com/qemu/qemu/blob/master/hw/misc/debugexit.c
88+
write_io_port(0xf4, exit_code as u8)
89+
}
90+
7691
#[no_mangle]
7792
pub extern "C" fn efi_entry() -> ! {
93+
#[cfg(test)]
94+
test_main();
95+
7896
use core::fmt::Write;
7997
com_initialize(IO_ADDR_COM2);
8098
let mut writer = Writer {};
@@ -84,3 +102,21 @@ pub extern "C" fn efi_entry() -> ! {
84102
n += 1;
85103
}
86104
}
105+
106+
#[cfg(test)]
107+
fn test_runner(tests: &[&dyn Fn()]) {
108+
use core::fmt::Write;
109+
com_initialize(IO_ADDR_COM2);
110+
let mut writer = Writer {};
111+
write!(writer, "Running {} tests...", tests.len());
112+
for test in tests {
113+
test();
114+
}
115+
write!(writer, "Done!");
116+
exit_qemu(QemuExitCode::Success)
117+
}
118+
119+
#[test_case]
120+
fn trivial_assertion() {
121+
assert_eq!(1, 1);
122+
}

ovmf/bios64.bin

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)