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_native_sdhci_test.sh

125 lines · 4866 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_native_sdhci_test.sh -- boot a bare-metal AArch64 payload that drives
18# QEMU's emulated PCI SD Host Controller and SD card with the C driver in
19# native/disk (disk_sdhci.c, the Pi 5 microSD driver).
20#
21# Two boots:
22# spec2-sdsc host controller spec 2.00, 64 MiB card (standard capacity,
23# byte addressing, power-of-two clock divider)
24# spec3-sdhc host controller spec 3.00, 4 GiB card (high capacity, block
25# addressing, 10-bit divided clock)
26# Each boot writes, reads back, flushes, checks range refusal, re-initialises
27# the controller and card and checks the data is still there. Afterwards this
28# script reads block 5 out of the card image file and checks the marker the
29# payload wrote, so the data is known to have reached the medium.
30#
31# Verdict line: AIENOS_DISK_SDHCI_QEMU: PASS | FAIL | NOT_RUN
32# QEMU is an emulator. PASS here qualifies nothing about physical hardware.
33#
34# Respects the shared quiet flag: refuses to run (NOT_RUN, exit 0) while
35# ~/workspace/.spark-quiet exists, which it only reads, or while another gate
36# holds the QEMU gate lock (scripts/lib_gate_hold.sh, aienos#278).
37set -euo pipefail
38
39repo="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
40qemu_timeout="${AIENOS_QEMU_TIMEOUT:-60}"
41out_rel="out"
42out="${repo}/native/disk/qemu/${out_rel}"
43. "${repo}/scripts/lib_gate_hold.sh"
44
45if ! gh_quiet_check; then
46 echo "NOT_RUN ${gh_why}"
47 echo "AIENOS_DISK_SDHCI_QEMU: NOT_RUN"
48 exit 0
49fi
50for tool in qemu-system-aarch64 timeout make truncate dd; do
51 if ! command -v "${tool}" >/dev/null 2>&1; then
52 echo "required tool ${tool} not found"
53 echo "AIENOS_DISK_SDHCI_QEMU: NOT_RUN"
54 exit 0
55 fi
56done
57
58# One QEMU gate at a time; the gate lock record is removed only if it is still
59# this run's own (never another run's hold).
60if ! gh_lock_take qemu_native_sdhci_test "${AIENOS_GATE_MINUTES:-60}"; then
61 echo "NOT_RUN ${gh_why}"
62 echo "AIENOS_DISK_SDHCI_QEMU: NOT_RUN"
63 exit 0
64fi
65cleanup() { gh_lock_release; }
66trap cleanup EXIT
67
68cross=""
69if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi
70if ! make -s -C "${repo}/native/disk/qemu" OUT="${out_rel}" \
71 CC="${cross}gcc" LD="${cross}ld" NM="${cross}nm" >"${repo}/native/disk/qemu/build.log" 2>&1; then
72 tail -20 "${repo}/native/disk/qemu/build.log"
73 echo "payload build failed"
74 echo "AIENOS_DISK_SDHCI_QEMU: FAIL"
75 exit 1
76fi
77rm -f "${repo}/native/disk/qemu/build.log"
78
79mark="AIENOS-SDHCI-QEMU block 5 written by disk_sdhci.c"
80verdict=0
81run_one() { # name spec-version image-size
82 local name="$1" spec="$2" size="$3"
83 local img="${out}/sd-${name}.img" log="${out}/qemu-${name}.log" qrc
84 rm -f "${img}" "${log}"
85 truncate -s "${size}" "${img}"
86 echo "== ${name}: sd-spec-version=${spec}, card ${size}"
87 set +e
88 timeout --kill-after=5 "${qemu_timeout}" qemu-system-aarch64 \
89 -M virt,highmem=off -cpu cortex-a57 -m 256M -smp 1 \
90 -nographic -monitor none -serial stdio -no-reboot \
91 -kernel "${out}/sdhci_payload.elf" \
92 -drive "file=${img},if=none,id=aienos_sd,format=raw" \
93 -device "sdhci-pci,sd-spec-version=${spec}" \
94 -device sd-card,drive=aienos_sd \
95 </dev/null >"${log}" 2>&1
96 qrc=$?
97 set -e
98 tr -d '\r' <"${log}" | grep -v '^$' | tail -12
99 if [ "${qrc}" -eq 124 ] || [ "${qrc}" -eq 137 ]; then
100 echo "QEMU did not power off within ${qemu_timeout} s"
101 fi
102 if ! tr -d '\r' <"${log}" | grep -qx 'SDHCI_QEMU_PAYLOAD: PASS' || [ "${qrc}" -ne 0 ]; then
103 echo "${name}: FAIL"
104 verdict=1
105 return
106 fi
107 if [ "$(dd if="${img}" bs=512 skip=5 count=1 status=none | head -c "${#mark}")" != "${mark}" ]; then
108 echo "${name}: marker not found in block 5 of the card image (FAIL)"
109 verdict=1
110 return
111 fi
112 echo "${name}: PASS (marker read back from the card image file)"
113}
114
115run_one spec2-sdsc 2 64M
116run_one spec3-sdhc 3 4G
117rm -f "${out}"/sd-*.img
118
119if [ "${verdict}" -eq 0 ]; then
120 echo "(QEMU emulated SD host controller and card only; not a physical-hardware qualification)"
121 echo "AIENOS_DISK_SDHCI_QEMU: PASS"
122 exit 0
123fi
124echo "AIENOS_DISK_SDHCI_QEMU: FAIL"
125exit 1