interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / scripts / qemu_ck_imac_diag_test.sh

135 lines · 7797 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# qemu_ck_imac_diag_test.sh -- QEMU test for IMACDIAG.EFI (AIENOS-IMAC-BOOT-0,
18# native/boot/diag_main.c + diag_x86.c). QEMU is an emulator: a PASS here
19# qualifies nothing about a 2017 iMac or any other real x86_64 machine.
20#
21# Builds IMACDIAG.EFI, puts it on a FAT image as EFI/BOOT/BOOTX64.EFI attached as a
22# SATA disk (q35 AHCI, like the iMac's internal drives), boots it in QEMU x86_64
23# with OVMF, waits for the app to power QEMU off, reads \EFI\AIENOS\IMACBOOT.TXT and
24# every \EFI\AIENOS\ACPI\*.BIN back with mtools and checks the firmware, CPU,
25# SMBIOS, PCI, screen and disk lines, the ACPI copies, "DONE", and that the app
26# wrote the file and asked for a shutdown.
27# Needs qemu-system-x86_64, OVMF, mtools and an x86_64 gcc (x86_64-linux-gnu- on
28# other hosts). Final line:
29# AIENOS_IMAC_DIAG: PASS|FAIL|NOT_RUN [(...)] exit 0 / 1 / 2 (tools) / 3 (NOT_RUN)
30set -euo pipefail
31repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
32cd "${repo_root}"
33tag="AIENOS_IMAC_DIAG"
34code_fd="${OVMF_CODE:-/usr/share/OVMF/OVMF_CODE_4M.fd}"
35vars_fd="${OVMF_VARS:-/usr/share/OVMF/OVMF_VARS_4M.fd}"
36cross=""
37if [ "$(uname -m)" != "x86_64" ]; then cross="x86_64-linux-gnu-"; fi
38for t in qemu-system-x86_64 mformat mmd mcopy "${cross}gcc" "${cross}ld"; do
39 command -v "${t}" >/dev/null || { echo "${tag}: NOT_RUN (${t} missing)"; exit 2; }
40done
41[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "${tag}: NOT_RUN (no OVMF)"; exit 2; }
42out="${IMACDIAG_OUT:-${repo_root}/target/native-kernel-imac-diag}"
43make -s -C native/kernel X86_CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="$(git rev-parse HEAD 2>/dev/null || echo unknown)" imac-diag >/dev/null
44
45. "${repo_root}/scripts/lib_gate_hold.sh"
46if ! gh_quiet_check || ! gh_lock_take qemu_ck_imac_diag_test "${AIENOS_GATE_MINUTES:-60}"; then
47 echo "${tag}: NOT_RUN (${gh_why})"
48 exit 3
49fi
50work="$(mktemp -d)"
51cleanup() {
52 [[ -z "${qpid:-}" ]] || kill "${qpid}" 2>/dev/null || true
53 rm -rf "${work}"
54 gh_lock_release
55}
56trap cleanup EXIT
57
58img="${work}/esp.img"
59truncate -s 64M "${img}"
60mformat -i "${img}" -F ::
61mmd -i "${img}" ::/EFI ::/EFI/BOOT
62mcopy -i "${img}" "${out}/IMACDIAG.EFI" ::/EFI/BOOT/BOOTX64.EFI
63cp "${vars_fd}" "${work}/vars.fd"
64qemu-system-x86_64 -M q35 -accel tcg -cpu max -smp 2 -m 1024 \
65 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
66 -drive if=pflash,format=raw,file="${work}/vars.fd" \
67 -drive if=none,id=esp,format=raw,file="${img}" -device ide-hd,drive=esp,bus=ide.0 \
68 -display none -nic none -serial file:"${work}/serial.log" -no-reboot &
69qpid=$!
70done_seen=0
71self_exit=0
72for _ in $(seq 1 ${AIENOS_IMAC_DIAG_WAIT:-300}); do
73 grep -aq "AIENOS-IMAC-BOOT-0 DONE" "${work}/serial.log" 2>/dev/null && done_seen=1
74 if ! kill -0 "${qpid}" 2>/dev/null; then self_exit=1; break; fi
75 sleep 1
76done
77kill "${qpid}" 2>/dev/null || true
78wait "${qpid}" 2>/dev/null || true
79qpid=""
80
81fails=0
82chk() { # description, file, pattern
83 if grep -aqE -- "$3" "$2"; then echo "PASS $1"; else echo "FAIL $1 (no match for: $3)"; fails=$((fails + 1)); fi
84}
85tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt" 2>/dev/null || : >"${work}/serial.txt"
86[[ ${done_seen} == 1 ]] || { echo "FAIL serial never showed DONE"; fails=$((fails + 1)); }
87chk "serial: report_file written" "${work}/serial.txt" '^report_file: written '
88chk "serial: shutdown requested" "${work}/serial.txt" "^reset: shutdown\$"
89[[ ${self_exit} == 1 ]] && echo "PASS QEMU stopped by itself after the shutdown" || { echo "FAIL QEMU still running: ResetSystem did not power off"; fails=$((fails + 1)); }
90r="${work}/IMACBOOT.TXT"
91if mcopy -i "${img}" ::/EFI/AIENOS/IMACBOOT.TXT "${r}" 2>/dev/null; then
92 echo "PASS IMACBOOT.TXT read back from the image"
93 chk "file: title" "${r}" '^AIENOS-IMAC-BOOT-0$'
94 chk "file: vendor line" "${r}" '^firmware_vendor: .+'
95 chk "file: UEFI spec revision" "${r}" '^uefi_spec_revision: [0-9]+\.[0-9]+$'
96 chk "file: CPU vendor" "${r}" '^cpu_vendor: (GenuineIntel|AuthenticAMD)$'
97 chk "file: CPU brand" "${r}" '^cpu_brand: .+'
98 chk "file: CPU signature" "${r}" '^cpu_signature: family=0x[0-9a-f]+ model=0x[0-9a-f]+ stepping=[0-9]+ '
99 chk "file: CPU features include long mode and SSE2" "${r}" '^cpu_features: .*\bsse2\b.*\blm\b'
100 chk "file: CPU mode is ring 0, paged, long mode" "${r}" '^cpu_mode: cpl=0 paging=1 long_mode=1 '
101 chk "file: memory summary" "${r}" '^memory_map: entries=[0-9]+ conventional_mib=[0-9]+ map_key=0x[0-9a-f]+$'
102 chk "file: ACPI 2.0 table line" "${r}" '^table [0-9]+: .* ACPI 2\.0 at 0x'
103 chk "file: RSDP line" "${r}" "^acpi_rsdp: rev=[0-9]+ .* xsdt=0x[0-9a-f]+\$"
104 chk "file: MADT copied" "${r}" "^acpi_table: APIC .* checksum=ok via XSDT file=APIC.BIN written\$"
105 chk "file: DSDT copied via FADT" "${r}" "^acpi_table: DSDT .* checksum=ok via FADT file=DSDT.BIN written\$"
106 chk "file: no table failed" "${r}" "^acpi_dump: copied=[0-9]+ failed=0\$"
107 chk "file: SMBIOS entry" "${r}" '^smbios: entry=[23] version=[0-9]+\.[0-9]+ '
108 chk "file: SMBIOS system identity" "${r}" '^smbios_system: manufacturer=.+ product=.+ version='
109 # q35: host bridge 8086:29c0 at 00:00.0 and the ICH9 AHCI controller 8086:2922 at 00:1f.2.
110 chk "file: PCI host bridge" "${r}" '^pci: 00:00\.0 8086:29c0 .* host-bridge$'
111 chk "file: PCI SATA (AHCI) controller" "${r}" '^pci: 00:1f\.2 8086:2922 rev=[0-9a-f]{2} class=010601 .* storage-sata$'
112 chk "file: PCI display" "${r}" '^pci: .* class=0300[0-9a-f]{2} .* display-vga$'
113 chk "file: PCI function count" "${r}" '^pci_functions: [1-9][0-9]*$'
114 chk "file: GOP mode" "${r}" '^gop [0-9]+: mode=[0-9]+/[0-9]+ [1-9][0-9]*x[1-9][0-9]* '
115 chk "file: the 64 MiB boot disk" "${r}" '^block [0-9]+: disk removable=[01] present=1 read_only=0 block_bytes=512 blocks=131072 mib=64$'
116 chk "file: DONE" "${r}" '^AIENOS-IMAC-BOOT-0 DONE$'
117 if grep -aqiE '^smbios.*serial' "${r}"; then echo "FAIL an SMBIOS line mentions a serial number field"; fails=$((fails + 1)); else echo "PASS no SMBIOS line carries a serial number field"; fi
118 # Every table file named in the report is on the image, starts with its
119 # signature and is exactly the length the report and its own header give.
120 while read -r sig len file; do
121 if mcopy -n -i "${img}" "::/EFI/AIENOS/ACPI/${file}" "${work}/${file}" 2>/dev/null \
122 && [[ "$(head -c 4 "${work}/${file}")" == "${sig}" ]] \
123 && [[ "$(stat -c %s "${work}/${file}")" == "${len}" ]] \
124 && [[ "$(od -An -tu4 -j4 -N4 "${work}/${file}" | tr -d " ")" == "${len}" ]]; then
125 echo "PASS ACPI/${file}: signature ${sig}, ${len} bytes"
126 else
127 echo "FAIL ACPI/${file}: missing, wrong signature or wrong length"; fails=$((fails + 1))
128 fi
129 done < <(sed -nE "s/^acpi_table: ([A-Z0-9_]{4}) rev=[0-9]+ len=([0-9]+) .* file=([A-Z0-9_]+\.BIN) written\$/\1 \2 \3/p" "${r}")
130 echo "---- IMACBOOT.TXT ----"; cat "${r}"; echo "----"
131else
132 echo "FAIL IMACBOOT.TXT not found in the image"; fails=$((fails + 1))
133fi
134if [[ ${fails} == 0 ]]; then echo "${tag}: PASS"; exit 0; fi
135echo "${tag}: FAIL (${fails} checks)"; exit 1