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
omega-mv / tests / a64efi / run.sh
| 1 | #!/bin/sh |
| 2 | # tests/a64efi/run.sh [OMC]: the x86efi test programs (tests/x86efi) compiled for `target a64efi` with |
| 3 | # OMC (default seed/out/omc) and seed/a64efi/efirt.om, each booted as \EFI\BOOT\BOOTAA64.EFI on a fresh |
| 4 | # FAT disk under QEMU virt + EDK2. With KVM on an AArch64 host the CPU is the host's; elsewhere QEMU |
| 5 | # emulates one (-cpu max). Every check prints PASS, FAIL or NOT_RUN. Exit 1 on any FAIL; exit 77 when |
| 6 | # nothing could run (qemu-system-aarch64, the firmware or mtools absent); else 0. Each boot is stopped |
| 7 | # after EFI_TIMEOUT seconds (default 20): the application has returned by then and the firmware sits |
| 8 | # in its menu. A64EFI_CODE overrides the firmware path. Results are emulated, not hardware evidence. |
| 9 | # The sources are x86efi's with the first line changed and efirt.om placed after it: one set of test |
| 10 | # programs for both targets. file11 (efi 11) and the CPUID parts of ident are x86efi only. |
| 11 | set -u |
| 12 | here=$(cd "$(dirname "$0")" && pwd) |
| 13 | root=$(cd "$here/../.." && pwd) |
| 14 | X=$root/tests/x86efi |
| 15 | omc=${1:-$root/seed/out/omc} |
| 16 | case $omc in /*) ;; *) omc=$PWD/$omc ;; esac |
| 17 | wait=${EFI_TIMEOUT:-20} |
| 18 | work=$(mktemp -d "${TMPDIR:-/tmp}/a64efi.XXXXXX") |
| 19 | trap 'rm -rf "$work"' EXIT |
| 20 | pass=0 fail=0 notrun=0 |
| 21 | ok() { pass=$((pass + 1)); echo "PASS $*"; } |
| 22 | bad() { fail=$((fail + 1)); echo "FAIL $*"; } |
| 23 | nr() { notrun=$((notrun + 1)); echo "NOT_RUN $*"; } |
| 24 | |
| 25 | code=${A64EFI_CODE:-} |
| 26 | for f in /usr/share/qemu-efi-aarch64/QEMU_EFI.fd /usr/share/edk2/aarch64/QEMU_EFI.fd /usr/share/AAVMF/QEMU_EFI.fd; do |
| 27 | [ -z "$code" ] && [ -f "$f" ] && code=$f |
| 28 | done |
| 29 | efi=yes why= |
| 30 | command -v qemu-system-aarch64 > /dev/null 2>&1 || { efi=no; why="qemu-system-aarch64 not found"; } |
| 31 | command -v mformat > /dev/null 2>&1 || { efi=no; why="mtools not found"; } |
| 32 | command -v timeout > /dev/null 2>&1 || { efi=no; why="timeout not found"; } |
| 33 | { [ -n "$code" ] && [ -f "$code" ]; } || { efi=no; why="EDK2 AArch64 firmware not found (set A64EFI_CODE)"; } |
| 34 | accel="-cpu max" |
| 35 | [ "$(uname -m)" = aarch64 ] && [ -w /dev/kvm ] && accel="-cpu host -enable-kvm" |
| 36 | |
| 37 | block() { tr -d '\r' < "$1" | sed 's/\x1b\[[0-9;?=]*[a-zA-Z]//g' | awk '/^==B$/ { on = 1; next } on && /^==/ { exit } on { print }'; } |
| 38 | |
| 39 | # boot NAME SOURCE...: the sources concatenated, `target x86efi` on line 1 replaced by `target a64efi` |
| 40 | # and efirt.om inserted after it; leaves $work/NAME/{serial.log,disk.img}. $smb is the -smbios value. |
| 41 | smb= |
| 42 | boot() { |
| 43 | name=$1; shift; d=$work/$name; mkdir -p "$d" |
| 44 | cat "$@" > "$d/src.om" |
| 45 | [ "$(sed -n 1p "$d/src.om")" = "target x86efi" ] || { bad "$name: first line is not target x86efi"; return 1; } |
| 46 | { echo "target a64efi"; cat "$root/seed/a64efi/efirt.om"; sed 1d "$d/src.om"; } > "$d/app.om" |
| 47 | 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 |
| 48 | dd if=/dev/zero of="$d/disk.img" bs=1048576 count=64 2> /dev/null |
| 49 | mformat -i "$d/disk.img" -F :: && mmd -i "$d/disk.img" ::/EFI ::/EFI/BOOT && mcopy -i "$d/disk.img" "$d/app.efi" ::/EFI/BOOT/BOOTAA64.EFI |
| 50 | # shellcheck disable=SC2086 |
| 51 | timeout -k 3 "$wait" qemu-system-aarch64 -M virt $accel -m 1024 -display none ${smb:+-smbios "$smb"} \ |
| 52 | -bios "$code" -drive if=none,id=d0,format=raw,file="$d/disk.img" -device virtio-blk-device,drive=d0 \ |
| 53 | -net none -serial file:"$d/serial.log" -monitor none -no-reboot < /dev/null > /dev/null 2>&1 |
| 54 | return 0 |
| 55 | } |
| 56 | # trapped LOG: EDK2 reports the image's EFI_STATUS 2^63|103 (trap 3); the text is checked in `controls`. |
| 57 | trapped() { tr -d '\r' < "$1" | grep -q 'failed to start .*: 00000067\|Status = 8000000000000067\|8000000000000067'; } |
| 58 | diskfile() { mtype -i "$1" "::/$2" 2> /dev/null; } |
| 59 | |
| 60 | # Compile-time refusals and the default target, no firmware needed. refuse NAME WANT SOURCE: omc must |
| 61 | # print exactly `omc: line L: error E` (WANT is "L E") and exit with status E. |
| 62 | refuse() { |
| 63 | printf '%b' "$3" > "$work/r.om" |
| 64 | got=$("$omc" < "$work/r.om" 2>&1 > /dev/null); rc=$? |
| 65 | if [ $rc = "${2#* }" ] && [ "$got" = "omc: line ${2% *}: error ${2#* }" ]; then ok "refusal $1: $got"; else bad "refusal $1: exit $rc '$got', wanted line ${2% *} error ${2#* }"; fi |
| 66 | } |
| 67 | refuse "Linux host word" "3 25" 'target a64efi\nfn main to int\nx = getpid\nret 0\nend\n' |
| 68 | refuse "function mode" "1 25" 'target a64efi\nfn f a ulong to ulong\nret a\nend\n' |
| 69 | refuse "run-time pool" "2 25" 'target a64efi\nglobal p uchar *\nfn main to int\nret 0\nend\n' |
| 70 | refuse "efi without fn efirt" "4 16" 'target a64efi\nglobal b uchar 8\nfn main to int\nr = efi 13 0 b 0 1\nret 0\nend\n' |
| 71 | refuse "efi OP above 14" "7 25" 'target a64efi\nglobal b uchar 8\nfn efirt o ulong a ulong p ulong n ulong to ulong\nret 0\nend\nfn main to int\nr = efi 15 0 b 0 1\nret 0\nend\n' |
| 72 | refuse "om2 before the target line" "2 16" 'om2\ntarget a64efi\nfn main to int\nret 0\nend\n' |
| 73 | refuse "unknown target" "1 25" 'target a64uefi\nfn main to int\nret 0\nend\n' |
| 74 | refuse "read" "4 25" 'target a64efi\nglobal b uchar 8\nfn main to int\nk = read 0 b 0 1\nret 0\nend\n' |
| 75 | refuse "write" "4 25" 'target a64efi\nglobal b uchar 8\nfn main to int\nk = write 1 b 0 1\nret 0\nend\n' |
| 76 | refuse "function named fwld" "2 16" 'target a64efi\nfn fwld to ulong\nret 7\nend\nfn main to int\nret 0\nend\n' |
| 77 | refuse "variable named fwld" "3 16" 'target a64efi\nfn main to int\nfwld = ulong 3\nret 0\nend\n' |
| 78 | refuse "global named efi" "2 16" 'target a64efi\nglobal efi uchar 8\nfn main to int\nret 0\nend\n' |
| 79 | refuse "parameter named fwadr" "2 16" 'target a64efi\nfn f fwadr ulong to ulong\nret 0\nend\nfn main to int\nret 0\nend\n' |
| 80 | refuse "efirt without parameters" "7 9" 'target a64efi\nglobal b uchar 8\nfn efirt to ulong\nret 0\nend\nfn main to int\nr = efi 13 0 b 0 1\nret 0\nend\n' |
| 81 | refuse "efirt giving int" "7 9" 'target a64efi\nglobal b uchar 8\nfn efirt o ulong a ulong p ulong n ulong to int\nret 0\nend\nfn main to int\nr = efi 13 0 b 0 1\nret 0\nend\n' |
| 82 | refuse "efirt with a uchar parameter" "7 9" 'target a64efi\nglobal b uchar 8\nfn efirt o ulong a uchar p ulong n ulong to ulong\nret 0\nend\nfn main to int\nr = efi 13 0 b 0 1\nret 0\nend\n' |
| 83 | refuse "efirt with an array parameter" "7 9" 'target a64efi\nglobal b uchar 8\nfn efirt o ulong a ulong p view uchar 8 n ulong to ulong\nret 0\nend\nfn main to int\nr = efi 13 0 b 0 1\nret 0\nend\n' |
| 84 | refuse "efiexit with parameters" "7 9" 'target a64efi\nfn efiexit a ulong b ulong to void\nend\nfn main to int\nret 0\nend\n' |
| 85 | refuse "efiexit giving a value" "8 9" 'target a64efi\nfn efiexit to ulong\nret 99\nend\nfn main to int\nret 0\nend\n' |
| 86 | # Without the target line the six words are ordinary names: a program may define fwld and a variable efi. |
| 87 | printf '%b' 'fn fwld a ulong b ulong to ulong\nefi = add a b\nret efi\nend\nfn main to int\nret int fwld 40 2\nend\n' > "$work/n.om" |
| 88 | if "$omc" < "$work/n.om" > "$work/n" 2> /dev/null && chmod +x "$work/n"; then |
| 89 | if [ "$(uname -m)" = aarch64 ]; then "$work/n"; r=$?; [ $r = 42 ] && ok "default target: fwld and efi are ordinary names (exit 42)" || bad "default target: fwld program exit $r" |
| 90 | else nr "default target fwld program: not an AArch64 host"; fi |
| 91 | else bad "default target: a program defining fwld was refused"; fi |
| 92 | |
| 93 | [ $efi = yes ] || { nr "all: $why"; echo "SUMMARY pass=$pass fail=$fail not_run=$notrun"; exit 77; } |
| 94 | |
| 95 | # float: the body of tests/x86efi/float gives the bit patterns of x86efi, x86linux and AArch64 Linux. |
| 96 | F=$X/float |
| 97 | if boot float "$F/efi-pre.om" "$F/body.om" "$F/fexplog.om" "$F/main-efi.om"; then |
| 98 | block "$work/float/serial.log" | cmp -s - "$F/expected.txt" && ok "float a64efi: $(wc -l < "$F/expected.txt") lines match x86efi's expected.txt" || bad "float a64efi differs" |
| 99 | fi |
| 100 | # refusals: every efi 12 refusal code, then an OFF+N past the array traps 3. |
| 101 | if boot refusals "$X/refusals/g4.om"; then |
| 102 | block "$work/refusals/serial.log" | cmp -s - "$X/refusals/expected.txt" && ok "refusals: $(wc -l < "$X/refusals/expected.txt") results match" || bad "refusals differ" |
| 103 | trapped "$work/refusals/serial.log" && ok "refusals: OFF+N past the array ends in trap 3" || bad "refusals: no trap 3 status" |
| 104 | fi |
| 105 | # file: create, write, close, reopen, positioned write, read back. |
| 106 | if boot file "$X/file/t1.om"; then |
| 107 | block "$work/file/serial.log" | cmp -s - "$X/file/expected.txt" && ok "file: round trip results match" || bad "file: results differ" |
| 108 | [ "$(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" |
| 109 | fi |
| 110 | # exit: a file left open is flushed and closed by return (e1) and by a trap (e2), through fn efiexit. |
| 111 | for e in e1 e2; do |
| 112 | if boot "exit-$e" "$X/exit/$e.om"; then |
| 113 | s=$work/exit-$e/serial.log got=$(diskfile "$work/exit-$e/disk.img" X.TXT) |
| 114 | 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 |
| 115 | if [ ! -s "$s" ]; then bad "exit $e: no serial log" |
| 116 | elif [ $e = e1 ]; then |
| 117 | if trapped "$s"; then bad "exit e1: unexpected trap"; else ok "exit e1: returned without a trap"; fi |
| 118 | elif trapped "$s"; then ok "exit e2: ended in trap 3" |
| 119 | else bad "exit e2: no trap 3 status"; fi |
| 120 | fi |
| 121 | done |
| 122 | # ident: efi 14 sub 3 reads the SMBIOS type 1 strings the firmware was given and never the serial or the |
| 123 | # UUID. The brand line (CPUID) is x86 only and is not compared. Negative control: another manufacturer. |
| 124 | I=$X/ident |
| 125 | smb=type=1,manufacturer=OmegaTest,product=IdentBox,version=7.1,serial=SECRET123,uuid=12345678-1234-1234-1234-123456789abc |
| 126 | if boot ident "$I/id.om"; then |
| 127 | block "$work/ident/serial.log" | grep -qx 'sm=OmegaTest/IdentBox/7.1' && ok "ident: SMBIOS type 1 strings read (sm=OmegaTest/IdentBox/7.1)" || bad "ident: no line sm=OmegaTest/IdentBox/7.1" |
| 128 | tr -d '\r' < "$work/ident/serial.log" | grep -q 'SECRET123\|12345678' && bad "ident: serial or UUID printed" || ok "ident: no serial or UUID in the output" |
| 129 | fi |
| 130 | smb=type=1,manufacturer=OtherMaker,product=IdentBox,version=7.1 |
| 131 | if boot ident-neg "$I/id.om"; then |
| 132 | block "$work/ident-neg/serial.log" | grep -qx 'sm=OtherMaker/IdentBox/7.1' && ok "ident negative control: OtherMaker is reported" || bad "ident negative control: OtherMaker not reported" |
| 133 | fi |
| 134 | smb= |
| 135 | # om2: `target a64efi` then `om2`, efirt.om and tests/a64efi/om2.om: an OM-2 form and globals, no pinned register |
| 136 | d=$work/om2; mkdir -p "$d" |
| 137 | { echo "target a64efi"; echo om2; cat "$root/seed/a64efi/efirt.om" "$here/om2.om"; } > "$d/app.om" |
| 138 | if "$omc" < "$d/app.om" > "$d/app.efi" 2> "$d/omc.err"; then |
| 139 | dd if=/dev/zero of="$d/disk.img" bs=1048576 count=64 2> /dev/null |
| 140 | mformat -i "$d/disk.img" -F :: && mmd -i "$d/disk.img" ::/EFI ::/EFI/BOOT && mcopy -i "$d/disk.img" "$d/app.efi" ::/EFI/BOOT/BOOTAA64.EFI |
| 141 | # shellcheck disable=SC2086 |
| 142 | timeout -k 3 "$wait" qemu-system-aarch64 -M virt $accel -m 1024 -display none -bios "$code" \ |
| 143 | -drive if=none,id=d0,format=raw,file="$d/disk.img" -device virtio-blk-device,drive=d0 \ |
| 144 | -net none -serial file:"$d/serial.log" -monitor none -no-reboot < /dev/null > /dev/null 2>&1 |
| 145 | [ "$(block "$d/serial.log")" = "om2 7 26" ] && ok "om2: an OM-2 unit under target a64efi prints om2 7 26" || bad "om2: printed '$(block "$d/serial.log")'" |
| 146 | else bad "om2: omc refused it: $(cat "$d/omc.err")"; fi |
| 147 | # no pinned global under `target a64efi` then `om2`: a global declared before the first function is read |
| 148 | # through X18 on the default target (LDRB W1, [X18] is the word 39400241), and must not be here |
| 149 | printf 'om2\nglobal b uchar 8\nfn main to int\nx = ulong get b 0\nret int x\nend\n' > "$work/p.om" |
| 150 | { echo "target a64efi"; cat "$work/p.om"; } > "$work/pa.om" |
| 151 | if "$omc" < "$work/pa.om" > "$work/pa.efi" 2> /dev/null && "$omc" < "$work/p.om" > "$work/p.elf" 2> /dev/null; then |
| 152 | na=$(od -An -tx4 -v "$work/pa.efi" | tr ' ' '\n' | grep -c '^39400241$'); nd=$(od -An -tx4 -v "$work/p.elf" | tr ' ' '\n' | grep -c '^39400241$') |
| 153 | [ "$na" = 0 ] && [ "$nd" -ge 1 ] && ok "om2 pins: no load through X18 under target a64efi (the default target has $nd)" || bad "om2 pins: X18 loads a64efi=$na default=$nd" |
| 154 | else bad "om2 pins: omc refused the units"; fi |
| 155 | # scrub under `target a64efi` then `om2` must not write X18 (MOVZ X18, #0 is the word d2800012); on the default |
| 156 | # target the same word is part of scrub, which shows the check sees it |
| 157 | printf 'om2\nfn main to int\nz = scrub 16\nret int z\nend\n' > "$work/s.om" |
| 158 | { echo "target a64efi"; cat "$work/s.om"; } > "$work/sa.om" |
| 159 | if "$omc" < "$work/sa.om" > "$work/sa.efi" 2> /dev/null && "$omc" < "$work/s.om" > "$work/s.elf" 2> /dev/null; then |
| 160 | na=$(od -An -tx4 -v "$work/sa.efi" | tr ' ' '\n' | grep -c '^d2800012$'); nd=$(od -An -tx4 -v "$work/s.elf" | tr ' ' '\n' | grep -c '^d2800012$') |
| 161 | n19=$(od -An -tx4 -v "$work/sa.efi" | tr ' ' '\n' | grep -c '^d2800013$') |
| 162 | [ "$na" = 0 ] && [ "$n19" -ge 1 ] && [ "$nd" -ge 1 ] && ok "scrub: no write of X18 under target a64efi (X19 cleared; the default target clears X18)" || bad "scrub: X18 words a64efi=$na default=$nd, X19 words a64efi=$n19" |
| 163 | else bad "scrub: omc refused the scrub units"; fi |
| 164 | nr "file11: efi 11 is x86efi only" |
| 165 | echo "SUMMARY pass=$pass fail=$fail not_run=$notrun" |
| 166 | [ $fail = 0 ] |