Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/scripts/qemu_ck_disk_layout_test.sh # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / scripts / qemu_ck_disk_layout_test.sh` 281 lines ยท 15063 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 # DISK_LAYOUT gate for the AIENOS C kernel (CK-4, audit row R2 of 18 # track4-aienos-trust.md): the kernel writes to a disk only inside the one 19 # partition whose GPT type GUID is the AIENOS type 20 # 38DAAC89-5EAD-4B40-8B1E-3687A7418061 (native/kernel/dev/disk_part.h), and 21 # every write goes through the bounds-checked translation layer ck_part_xlate. 22 # QEMU AArch64 + UEFI (AAVMF) + NVMe, same base invocation as 23 # scripts/qemu_ck_store_test.sh. QEMU is not hardware: a PASS here qualifies 24 # nothing physical. 25 # 26 # Images (native/kernel/Makefile): 27 # - make full: the default (safe) image, booted with an SMMUv3. 28 # - make full CK_TEST_DISK_XLATE_BYPASS=1: TEST-ONLY mutation image whose 29 # translation layer drops the partition offset and checks only the whole 30 # disk's bounds (refused under CK_HARDWARE_STAGING by the Makefile and by 31 # an #error in dev/disk_part.h). It must make this gate's positive checks 32 # FAIL; it never counts toward a PASS. 33 # Disk images: native/kernel/tools/ck_gpt_image.c, 64 MiB: protective MBR, 34 # primary + backup GPT, sentinel-a [1,9) MiB filled, AIENOS [9,57) MiB all 35 # zero, sentinel-b [57,63) MiB filled. 36 # 37 # Positive (default image): one 512 B disk booted twice, one 4096 B disk booted 38 # once. Each boot: M1 checks (scripts/lib_ck_m1_checks.sh); "disk: gpt ok" 39 # with the AIENOS range the image tool wrote; partition LBA 0 -> disk LBA = 40 # partition start; write past the partition end refused; the NVMe rw probe's 41 # disk LBA inside the partition; stage store ok and a Store commit; every 42 # byte outside the partition unchanged (sha256 with the partition cut out) 43 # and the partition itself changed (not all zero any more); no bypass text. 44 # Hostile (default image, 512 B, each a fresh disk): no-gpt (no protective 45 # MBR, no GPT), bad CRC32 on BOTH headers, bad entry-array CRC32 on BOTH 46 # arrays, AIENOS partition ending past the disk, no AIENOS partition. Each: 47 # the host tool's own parser run ("ck_gpt_image find") refuses with the same 48 # reason; the kernel prints "disk: no AIENOS partition, refusing writes 49 # (gpt: , ...)", no "disk: gpt ok", no rw probe, the Store is 50 # refused with no boot disk, and the whole image sha256 is unchanged. 51 # Firmware caveat: EDK2/AAVMF PartitionDxe may repair a GPT when only ONE 52 # copy is bad (it rewrites it from the good copy), so the QEMU hostile images 53 # corrupt both copies. Single-copy corruption (bad primary CRC alone, bad 54 # primary entries alone, bad backup header or entries alone) is covered 55 # without firmware by the host test dev/tests/disk_part_test.c 56 # (make -C native/kernel stage-test). 57 # Mutation: the bypass image on a fresh good 512 B disk must announce itself 58 # and must FAIL the positive checks (its translation self-check reports 59 # "NOT REFUSED", so the Store is never reached); the default image file must 60 # not contain the bypass warning text. 61 # 62 # Reads the machine quiet flag and takes the QEMU gate lock like 63 # qemu_ck_store_test.sh (scripts/lib_gate_hold.sh). Any QEMU exit status other than 0 64 # fails (via the M1 checks). Final line: AIENOS_CK_DISK_LAYOUT: 65 # PASS|FAIL|NOT_RUN. Needs qemu-system-aarch64 and AAVMF. 66 set -euo pipefail 67 68 repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" 69 cd "${repo_root}" 70 71 verdicts() { echo "AIENOS_CK_DISK_LAYOUT: $1"; } 72 code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" 73 vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" 74 command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; verdicts NOT_RUN; exit 2; } 75 [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; verdicts NOT_RUN; exit 2; } 76 77 cross="" 78 if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi 79 commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" 80 out="${repo_root}/target/native-kernel" 81 make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" full CK_TEST_DISK_XLATE_BYPASS=1 >/dev/null 82 make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" full >/dev/null 83 make -s -C native/kernel OUT="${out}" gpt-image >/dev/null 84 gpt_tool="${out}/host/ck_gpt_image" 85 safe_efi="${out}/full/BOOTAA64.EFI" 86 bypass_efi="${out}/full-test-xlate-bypass/BOOTAA64.EFI" 87 88 # Machine quiet flag: one heavy run at a time on the Spark. 89 # Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): 90 # scripts/lib_gate_hold.sh (aienos#278). 91 . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" 92 if ! gh_quiet_check || ! gh_lock_take "qemu_ck_disk_layout_test" "${AIENOS_GATE_MINUTES:-60}"; then 93 echo "NOT_RUN ${gh_why}" 94 verdicts NOT_RUN 95 exit 3 96 fi 97 # Release only this run's own gate lock record, at most once. 98 release_flag() { gh_lock_release; } 99 top="$(mktemp -d)" 100 cleanup() { 101 rm -rf "${top}" 102 release_flag 103 } 104 trap cleanup EXIT 105 106 # shellcheck source=scripts/lib_ck_m1_checks.sh 107 source "${repo_root}/scripts/lib_ck_m1_checks.sh" 108 mib=64 109 gate_fail=0 110 fail_into_gate() { if [[ "${failed}" != 0 ]]; then gate_fail=1; fi; failed=0; } 111 sha() { sha256sum "$1" | cut -d' ' -f1; } 112 113 # new_disk IMG BS LAYOUT [CORRUPTION...]: create it; sets part_first/part_last 114 # (the AIENOS range as WRITTEN by the tool; 0 0 when the layout has none). 115 new_disk() { 116 local img="$1" bs="$2" lay="$3" l re='^gpt_image bs=[0-9]+ blocks=[0-9]+ aienos_first_lba=([0-9]+) aienos_last_lba=([0-9]+)$' 117 shift 3 118 l=$("${gpt_tool}" create "${img}" "${bs}" "${mib}" "${lay}" "$@") || l="" 119 if [[ "${l}" =~ ${re} ]]; then part_first="${BASH_REMATCH[1]}"; part_last="${BASH_REMATCH[2]}" 120 else echo "FAIL GPT disk image ${lay} not created (${l})"; part_first=0; part_last=0; gate_fail=1; fi 121 } 122 # outside_sha IMG BS: sha256 of the image with the AIENOS partition cut out. 123 outside_sha() { 124 { head -c $(( part_first * $2 )) "$1"; tail -c +$(( (part_last + 1) * $2 + 1 )) "$1"; } | sha256sum | cut -d' ' -f1 125 } 126 # inside_sha IMG BS: sha256 of the AIENOS partition bytes only. 127 inside_sha() { 128 # head reads the file itself and tail drains all of head's output, so no 129 # producer is cut off early (a "tail | head" pipe dies of SIGPIPE under pipefail). 130 head -c $(( (part_last + 1) * $2 )) "$1" | tail -c $(( (part_last - part_first + 1) * $2 )) | sha256sum | cut -d' ' -f1 131 } 132 # zero_sha BS: sha256 of an all-zero AIENOS partition of the current size. 133 zero_sha() { 134 head -c $(( (part_last - part_first + 1) * $1 )) /dev/zero | sha256sum | cut -d' ' -f1 135 } 136 137 # boot : one QEMU boot with an SMMUv3; 138 # leaves the serial text in ${work}/serial.txt and the exit status in 139 # qemu_status, then runs the M1 checks (into failed). 140 boot() { 141 work="${top}/$1" 142 mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS" 143 touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT" 144 cp "$2" "${work}/esp/EFI/BOOT/BOOTAA64.EFI" 145 cp "${vars_fd}" "${work}/vars.fd" 146 local nvme_dev="nvme,drive=nvme0,serial=aienos-disk-layout" 147 [[ "$4" == 512 ]] || nvme_dev="${nvme_dev},logical_block_size=$4,physical_block_size=$4" 148 set +e 149 # Issue #61: single-threaded TCG (see qemu_boot_test.sh). 150 timeout "${AIENOS_QEMU_TIMEOUT:-180}" qemu-system-aarch64 \ 151 -M virt,virtualization=on,gic-version=3,iommu=smmuv3 -accel tcg,thread=single -cpu max -smp 4 -m 2048 \ 152 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ 153 -drive if=pflash,format=raw,file="${work}/vars.fd" \ 154 -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \ 155 -device virtio-blk-pci,drive=esp \ 156 -drive if=none,id=nvme0,format=raw,file="$3" \ 157 -device "${nvme_dev}" \ 158 -device ramfb -display none -nic none \ 159 -serial file:"${work}/serial.log" -no-reboot 160 qemu_status=$? 161 set -e 162 tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt" 163 [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/qemu_ck_disk_layout_$1.log" 164 echo "== boot $1: qemu exit ${qemu_status}" 165 failed=0 166 ck_m1_checks 167 } 168 169 # positive_checks BS IMG OUTSIDE_REF ZERO_INSIDE_REF FIRST_BOOT(1|0): the 170 # partition-aware footprint on a good disk. Sets failed=1 on any failure. 171 positive_checks() { 172 local bs="$1" img="$2" oref="$3" zref="$4" first="$5" 173 check "GPT parsed (primary+backup CRC32), AIENOS partition found where the image tool wrote it" \ 174 "^disk: gpt ok primary+backup crc32 entries=128 used=3 aienos_index=1 first_lba=${part_first} last_lba=${part_last} " 175 check "partition LBA 0 translates to the partition start" "^disk: xlate part_lba=0 -> disk_lba=${part_first} (rc=0)$" 176 check "write past the partition end refused by the translation layer" \ 177 "^disk: write past partition end part_lba=[0-9]* -> refused " 178 check_absent "no AIENOS-partition refusal on the good disk" "disk: no AIENOS partition" 179 local pre='^nvme: rw probe part_lba=([0-9]+) disk_lba=([0-9]+) bytes=4096 write\+flush\+readback match \(rc=0\)$' 180 local pl; pl=$(grep -E "${pre}" "${work}/serial.txt" | tail -1 || true) 181 if [[ "${pl}" =~ ${pre} ]] && (( BASH_REMATCH[2] == BASH_REMATCH[1] + part_first )) \ 182 && (( BASH_REMATCH[2] >= part_first && BASH_REMATCH[2] + 4096 / bs - 1 <= part_last )); then 183 echo "PASS rw probe landed inside the AIENOS partition (disk_lba ${BASH_REMATCH[2]})" 184 else 185 echo "FAIL rw probe not inside [${part_first},${part_last}] or not matched (${pl:-no line})"; failed=1 186 fi 187 check "NVMe bound by the devices stage" "devices: pci=ok nvme=bound" 188 check "stage devices ok" "stage devices: ok" 189 check "stage store ok" "stage store: ok" 190 check_absent "Store not refused" "store: REFUSED" 191 check "Store committed a generation on the partition" "^store: committed generation=[0-9]* boot_count=[0-9]*$" 192 if [[ "${first}" == 1 ]]; then 193 check "blank partition formatted as a TEST store" "store: blank disk (all-zero anchor + Store head): formatted TEST store" 194 else 195 check_absent "existing Store on the partition not reformatted" "formatted TEST store" 196 fi 197 local now; now=$(outside_sha "${img}" "${bs}") 198 [[ "${now}" == "${oref}" ]] && echo "PASS MBR, primary+backup GPT and both sentinel partitions byte-identical" \ 199 || { echo "FAIL bytes outside the AIENOS partition changed (${oref:0:16} -> ${now:0:16})"; failed=1; } 200 now=$(inside_sha "${img}" "${bs}") 201 [[ "${now}" != "${zref}" ]] && echo "PASS the AIENOS partition itself was written" \ 202 || { echo "FAIL the AIENOS partition is still all zero"; failed=1; } 203 } 204 205 # --- Positive ------------------------------------------------------------- 206 for bs in 512 4096; do 207 image="${top}/good-${bs}.img" 208 new_disk "${image}" "${bs}" aienos-middle 209 oref=$(outside_sha "${image}" "${bs}") 210 zref=$(zero_sha "${bs}") 211 [[ "$(inside_sha "${image}" "${bs}")" == "${zref}" ]] || { echo "FAIL fresh AIENOS partition not all zero"; gate_fail=1; } 212 nboots=1; [[ "${bs}" == 512 ]] && nboots=2 213 for (( k = 1; k <= nboots; k++ )); do 214 boot "good-${bs}-boot${k}" "${safe_efi}" "${image}" "${bs}" 215 positive_checks "${bs}" "${image}" "${oref}" "${zref}" "$(( k == 1 ))" 216 check_absent "no translation-bypass text in this image" "DISK TRANSLATION BYPASS" 217 fail_into_gate 218 done 219 done 220 221 # --- Hostile -------------------------------------------------------------- 222 # name | layout and corruptions | reason printed by ck_gpt_strerror (dev/disk_part.c) 223 hostile=( 224 "no-gpt|no-gpt|no protective MBR" 225 "bad-header-crc|aienos-middle bad-primary-crc bad-backup-crc|primary header CRC32 mismatch" 226 "bad-entry-crc|aienos-middle bad-primary-entries bad-backup-entries|entry array CRC32 mismatch" 227 "aienos-outside|aienos-outside|partition outside the usable range" 228 "no-aienos|no-aienos|no AIENOS partition type" 229 ) 230 for row in "${hostile[@]}"; do 231 IFS='|' read -r name spec reason <<<"${row}" 232 image="${top}/hostile-${name}.img" 233 # shellcheck disable=SC2086 # spec is a layout plus corruption words 234 new_disk "${image}" 512 ${spec} 235 host=$("${gpt_tool}" find "${image}" 512 || true) 236 [[ "${host}" == "refused: ${reason} rc="* ]] && echo "PASS host parser refuses ${name}: ${reason}" \ 237 || { echo "FAIL host parser on ${name}: expected '${reason}', got '${host}'"; gate_fail=1; } 238 before=$(sha "${image}") 239 boot "hostile-${name}" "${safe_efi}" "${image}" 512 240 check "NVMe LBA 0 read before the GPT decision" "nvme: read lba=0 blocks=1 ok" 241 check "${name}: refused, fail closed" "^disk: no AIENOS partition, refusing writes (gpt: ${reason}, rc=-4[0-9][0-9])$" 242 check_absent "${name}: no partition accepted" "^disk: gpt ok" 243 check_absent "${name}: no rw probe (nothing written)" "nvme: rw probe" 244 check "${name}: NVMe not bound" "devices: pci=ok nvme=unbound" 245 check "${name}: stage devices fails" "^stage devices: FAIL rc=-4[0-9][0-9]$" 246 check "${name}: Store refused without a boot disk" 'store: REFUSED proof=io step="no boot disk"' 247 check_absent "${name}: nothing committed" "store: committed" 248 after=$(sha "${image}") 249 [[ "${after}" == "${before}" ]] && echo "PASS ${name}: whole image byte-identical after the boot" \ 250 || { echo "FAIL ${name}: image changed (${before:0:16} -> ${after:0:16})"; failed=1; } 251 fail_into_gate 252 done 253 254 # --- Mutation ------------------------------------------------------------- 255 if grep -qaF "DISK TRANSLATION BYPASS" "${safe_efi}"; then 256 echo "FAIL default image contains the translation-bypass text"; gate_fail=1 257 else 258 echo "PASS default image has no translation-bypass text" 259 fi 260 image="${top}/mutant-512.img" 261 new_disk "${image}" 512 aienos-middle 262 oref=$(outside_sha "${image}" 512) 263 zref=$(zero_sha 512) 264 boot "mutant-xlate-bypass" "${bypass_efi}" "${image}" 512 265 check "mutant image announces the TEST-ONLY translation bypass" \ 266 "WARNING: TEST-ONLY DISK TRANSLATION BYPASS BUILD (CK_TEST_DISK_XLATE_BYPASS=1" 267 mutant_boot="${failed}" 268 fail_into_gate 269 echo "-- positive checks against the mutant (must FAIL):" 270 positive_checks 512 "${image}" "${oref}" "${zref}" 1 271 if [[ "${mutant_boot}" == 0 && "${failed}" != 0 ]]; then 272 echo "PASS translation bypass makes DISK_LAYOUT fail (mutant killed)" 273 else 274 echo "FAIL translation bypass mutant survived the positive checks (or did not boot cleanly)"; gate_fail=1 275 fi 276 failed=0 277 278 echo 279 if [[ "${gate_fail}" == 0 ]]; then verdicts PASS; exit 0; fi 280 verdicts FAIL 281 exit 1