interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / x86efi / run.sh

173 lines · 11151 bytes Raw Blame History
1#!/bin/sh
2# tests/x86efi/run.sh [OMC]: compile the x86efi tests with OMC (default seed/out/omc) and run each
3# image as \EFI\BOOT\BOOTX64.EFI on a fresh FAT disk under QEMU q35 + OVMF, serial console to a file.
4# Every check prints PASS, FAIL or NOT_RUN. Exit 1 on any FAIL; exit 77 when nothing could run
5# (qemu-system-x86_64, OVMF or mtools absent); else 0. Each QEMU boot is stopped after
6# EFI_TIMEOUT seconds (default 60): the application has returned by then and OVMF sits in its menu.
7# OVMF_CODE / OVMF_VARS override the firmware paths. Results are emulated, not hardware evidence.
8set -u
9here=$(cd "$(dirname "$0")" && pwd)
10root=$(cd "$here/../.." && pwd)
11omc=${1:-$root/seed/out/omc}
12case $omc in /*) ;; *) omc=$PWD/$omc ;; esac
13wait=${EFI_TIMEOUT:-60}
14work=$(mktemp -d "${TMPDIR:-/tmp}/x86efi.XXXXXX")
15trap 'rm -rf "$work"' EXIT
16pass=0 fail=0 notrun=0
17ok() { pass=$((pass + 1)); echo "PASS $*"; }
18bad() { fail=$((fail + 1)); echo "FAIL $*"; }
19nr() { notrun=$((notrun + 1)); echo "NOT_RUN $*"; }
20
21code=${OVMF_CODE:-} vars=${OVMF_VARS:-}
22for d in /usr/share/OVMF /usr/share/edk2/ovmf /usr/share/qemu; do
23 [ -z "$code" ] && [ -f "$d/OVMF_CODE_4M.fd" ] && code=$d/OVMF_CODE_4M.fd && vars=$d/OVMF_VARS_4M.fd
24 [ -z "$code" ] && [ -f "$d/OVMF_CODE.fd" ] && code=$d/OVMF_CODE.fd && vars=$d/OVMF_VARS.fd
25done
26efi=yes
27command -v qemu-system-x86_64 > /dev/null 2>&1 || { efi=no; why="qemu-system-x86_64 not found"; }
28command -v mformat > /dev/null 2>&1 || { efi=no; why="mtools not found"; }
29command -v timeout > /dev/null 2>&1 || { efi=no; why="timeout not found"; }
30{ [ -n "$code" ] && [ -f "$code" ] && [ -f "$vars" ]; } || { efi=no; why="OVMF firmware not found (set OVMF_CODE and OVMF_VARS)"; }
31
32# block FILE: the lines after the first "==B" up to the next line starting with "==".
33block() { tr -d '\r' < "$1" | awk '/^==B$/ { on = 1; next } on && /^==/ { exit } on { print }'; }
34
35# boot NAME SOURCE...: compile the concatenated sources, boot the image; leaves $work/NAME/{serial.log,disk.img}.
36# A $work/NAME/disk2.img made before the call is attached as a second USB stick.
37# $mach is the -machine value, $cpu the -cpu value, $smb the -smbios value (none when empty).
38mach=q35 cpu=max smb=
39boot() {
40 name=$1; shift; d=$work/$name; mkdir -p "$d"
41 cat "$@" > "$d/app.om"
42 if ! "$omc" < "$d/app.om" > "$d/app.efi" 2> "$d/omc.err"; then bad "$name: omc refused it: $(cat "$d/omc.err")"; return 1; fi
43 cp "$vars" "$d/vars.fd"
44 dd if=/dev/zero of="$d/disk.img" bs=1048576 count=16 2> /dev/null
45 mformat -i "$d/disk.img" :: && mmd -i "$d/disk.img" ::/EFI ::/EFI/BOOT && mcopy -i "$d/disk.img" "$d/app.efi" ::/EFI/BOOT/BOOTX64.EFI
46 second=
47 [ -f "$d/disk2.img" ] && second="-drive if=none,id=stick2,format=raw,file=$d/disk2.img -device usb-storage,bus=xhci.0,drive=stick2"
48 timeout "$wait" qemu-system-x86_64 -machine "$mach" -cpu "$cpu" -m 512 -display none ${smb:+-smbios "$smb"} \
49 -drive if=pflash,format=raw,readonly=on,file="$code" -drive if=pflash,format=raw,file="$d/vars.fd" \
50 -device qemu-xhci,id=xhci -drive if=none,id=stick,format=raw,file="$d/disk.img" \
51 -device usb-storage,bus=xhci.0,drive=stick $second -net none -serial file:"$d/serial.log" -no-reboot
52 return 0
53}
54# trapped LOG: OVMF reports the image's EFI_STATUS 2^63|103 (trap 3) as "...: 00000067".
55trapped() { tr -d '\r' < "$1" | grep -q 'failed to start .*: 00000067'; }
56diskfile() { mtype -i "$1" "::/$2" 2> /dev/null; }
57
58F=$here/float
59# float: the same body gives the same bit patterns as x86efi, x86linux and AArch64.
60if [ $efi = yes ]; then
61 if boot float "$F/efi-pre.om" "$F/body.om" "$F/fexplog.om" "$F/main-efi.om"; then
62 block "$work/float/serial.log" | cmp -s - "$F/expected.txt" && ok "float x86efi: $(wc -l < "$F/expected.txt") lines match" || bad "float x86efi differs"
63 fi
64else nr "float x86efi: $why"; fi
65host() { # host LABEL PRE RUNNER...
66 label=$1 pre=$2; shift 2; d=$work/$label; mkdir -p "$d"
67 cat "$pre" "$F/body.om" "$F/fexplog.om" "$F/main-lin.om" > "$d/app.om"
68 if ! "$omc" < "$d/app.om" > "$d/app" 2> "$d/omc.err"; then bad "$label: omc refused it"; return; fi
69 chmod +x "$d/app"; "$@" "$d/app" > "$d/out.txt"
70 printf '==E\n' >> "$d/out.txt"
71 block "$d/out.txt" | cmp -s - "$F/expected.txt" && ok "$label: same lines as x86efi" || bad "$label differs"
72}
73case $(uname -m) in
74 x86_64) host "float x86linux" "$F/lin-pre.om" env ;;
75 *) if command -v qemu-x86_64 > /dev/null 2>&1; then host "float x86linux (qemu-x86_64)" "$F/lin-pre.om" qemu-x86_64
76 else nr "float x86linux: not x86_64 and qemu-x86_64 not found"; fi ;;
77esac
78if [ "$(uname -m)" = aarch64 ]; then host "float aarch64" "$F/a64-pre.om" env; else nr "float aarch64: not an AArch64 host"; fi
79
80[ $efi = yes ] || { nr "refusals, file, exit: $why"; echo "SUMMARY pass=$pass fail=$fail not_run=$notrun"; [ $fail = 0 ] || exit 1; [ $pass = 0 ] && exit 77; exit 0; }
81
82# refusals: every efi 12 refusal code, then an OFF+N past the array traps 3.
83if boot refusals "$here/refusals/g4.om"; then
84 block "$work/refusals/serial.log" | cmp -s - "$here/refusals/expected.txt" && ok "refusals: $(wc -l < "$here/refusals/expected.txt") results match" || bad "refusals differ"
85 trapped "$work/refusals/serial.log" && ok "refusals: OFF+N past the array ends in trap 3" || bad "refusals: no trap 3 status"
86fi
87# file: create, write, close, reopen, positioned write, read back.
88if boot file "$here/file/t1.om"; then
89 block "$work/file/serial.log" | cmp -s - "$here/file/expected.txt" && ok "file: round trip results match" || bad "file: results differ"
90 [ "$(diskfile "$work/file/disk.img" T.TXT)" = abcd ] && ok "file: T.TXT on disk is abcd" || bad "file: T.TXT on disk is not abcd"
91fi
92# file11: efi 11 on two volumes: count and boot index, read the boot image, write it over the
93# other stick's 4-byte BOOTX64.EFI, read it back; volume 9 is error 19.
94d=$work/file11; mkdir -p "$d"; printf junk > "$d/junk"
95dd if=/dev/zero of="$d/disk2.img" bs=1048576 count=16 2> /dev/null
96mformat -i "$d/disk2.img" :: && mmd -i "$d/disk2.img" ::/EFI ::/EFI/BOOT && mcopy -i "$d/disk2.img" "$d/junk" ::/EFI/BOOT/BOOTX64.EFI
97if boot file11 "$here/file11/t1.om"; then
98 block "$d/serial.log" | cmp -s - "$here/file11/expected.txt" && ok "file11: results match" || bad "file11: results differ"
99 mcopy -n -i "$d/disk2.img" ::/EFI/BOOT/BOOTX64.EFI "$d/out.efi" 2> /dev/null
100 cmp -s "$d/out.efi" "$d/app.efi" && ok "file11: second stick's BOOTX64.EFI is the booted image" || bad "file11: second stick's BOOTX64.EFI is not the booted image"
101fi
102# exit: a file left open is flushed and closed by return (e1) and by a trap (e2). The status check
103# runs only for an image that booted, wrote a serial log and left the expected file on disk.
104for e in e1 e2; do
105 if boot "exit-$e" "$here/exit/$e.om"; then
106 s=$work/exit-$e/serial.log got=$(diskfile "$work/exit-$e/disk.img" X.TXT)
107 if [ "$got" = ABCDEFGHIJ ]; then ok "exit $e: X.TXT on disk is the 10 written bytes"; else bad "exit $e: X.TXT on disk is '$got'"; fi
108 if [ ! -s "$s" ]; then bad "exit $e: no serial log at $s"
109 elif [ "$got" != ABCDEFGHIJ ]; then bad "exit $e: status not checked, the disk check failed"
110 elif [ $e = e1 ]; then
111 if trapped "$s"; then bad "exit e1: unexpected trap"; else ok "exit e1: returned without a trap"; fi
112 elif trapped "$s"; then ok "exit e2: ended in trap 3"
113 else bad "exit e2: no trap 3 status"; fi
114 fi
115done
116# ident: efi 14 under both SMBIOS entry points (QEMU's 64-bit SMBIOS3 and the 32-bit _SM_). The type 1
117# record carries a serial and a UUID that the program must not print. Negative control: another
118# manufacturer must change the output, so the strings come from the firmware.
119I=$here/ident
120idsmb=type=1,manufacturer=OmegaTest,product=IdentBox,version=7.1,serial=SECRET123,uuid=12345678-1234-1234-1234-123456789abc
121for ep in 64 32; do
122 mach=q35,smbios-entry-point-type=$ep smb=$idsmb
123 if boot "ident-$ep" "$I/id.om"; then
124 block "$work/ident-$ep/serial.log" | cmp -s - "$I/expected.txt" && ok "ident SMBIOS $ep-bit: $(wc -l < "$I/expected.txt") lines match (brand, FirmwareVendor, type 1, results)" || bad "ident SMBIOS $ep-bit differs"
125 rev=$(tr -d '\r' < "$work/ident-$ep/serial.log" | sed -n 's/^==R \([0-9a-f]\{8\}\)$/\1/p')
126 [ -n "$rev" ] && ok "ident SMBIOS $ep-bit: FirmwareRevision $rev" || bad "ident SMBIOS $ep-bit: no FirmwareRevision line"
127 tr -d '\r' < "$work/ident-$ep/serial.log" | grep -q 'SECRET123\|12345678' && bad "ident SMBIOS $ep-bit: serial or UUID printed" || ok "ident SMBIOS $ep-bit: no serial or UUID in the output"
128 fi
129done
130mach=q35 smb=type=1,manufacturer=OtherMaker,product=IdentBox,version=7.1
131if boot ident-neg "$I/id.om"; then
132 if block "$work/ident-neg/serial.log" | cmp -s - "$I/expected.txt"; then bad "ident negative control: OtherMaker gave the expected output"
133 else block "$work/ident-neg/serial.log" | grep -qx 'sm=OtherMaker/IdentBox/7.1' && ok "ident negative control: OtherMaker differs from expected and is reported" || bad "ident negative control: OtherMaker not reported"; fi
134fi
135# ident bounds: a CPUID brand of only NULs or only blanks, and SMBIOS tables whose type 1 strings or
136# structures run past what id.om reads, must print - and finish (==E), never trap. Before the bounds
137# fix nul, blank, capwalk and capscan ended in trap 3 and n2 printed bytes of the next structure
138# (evidence/x86efi/ident-bounds-20261104/). The SMBIOS cases pass one raw file (QEMU puts it first
139# and generates no type 1 of its own when the file starts with one); handles are unique.
140o8() { printf "\\$(printf %03o "$1")"; }
141# t1 VER: type 1, handle 0x0100, Manufacturer 1, Product 2, Version VER, one string "OmegaTest".
142t1() { printf '\001\033\000\001\001\002'; o8 "$1"; printf '\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\006\000\000OmegaTest\000\000'; }
143# t11 H N: type 11 (OEM strings), handle 0x0B00+H, N strings of 60 characters.
144t11() {
145 printf '\013\005'; o8 "$1"; printf '\013'; o8 "$2"
146 s=$(printf '%060d' 0) i=0
147 while [ "$i" -lt "$2" ]; do printf '%s\000' "$s"; i=$((i + 1)); done
148 printf '\000'
149}
150t1 3 > "$work/n2.bin"
151{ t1 255; t11 1 100; t11 2 100; } > "$work/capwalk.bin"
152{ for h in 1 2 3 4 5; do t11 "$h" 255; done; } > "$work/capscan.bin"
153# bnd NAME WANT: the boot finished (==E), did not trap, and printed the line WANT.
154bnd() {
155 if trapped "$work/$1/serial.log"; then bad "ident bounds $1: trap 3"
156 elif ! tr -d '\r' < "$work/$1/serial.log" | grep -qx '==E'; then bad "ident bounds $1: no ==E line"
157 elif block "$work/$1/serial.log" | grep -qx -- "$2"; then ok "ident bounds $1: $2"
158 else bad "ident bounds $1: no line '$2'"; fi
159}
160mach=q35 cpu=max,model-id= smb=
161boot id-nul "$I/id.om" && bnd id-nul 'br=-'
162cpu="max,model-id=$(printf '%48s' '')"
163boot id-blank "$I/id.om" && bnd id-blank 'br=-'
164cpu=max mach=q35,smbios-entry-point-type=64
165smb=file=$work/n2.bin
166boot id-n2 "$I/id.om" && bnd id-n2 'sm=OmegaTest/-/-'
167smb=file=$work/capwalk.bin
168boot id-capwalk "$I/id.om" && bnd id-capwalk 'sm=OmegaTest/-/-'
169smb=file=$work/capscan.bin
170boot id-capscan "$I/id.om" && bnd id-capscan 'sm=-'
171mach=q35 cpu=max smb=
172echo "SUMMARY pass=$pass fail=$fail not_run=$notrun"
173[ $fail = 0 ]