CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / tests / gsp / run-fwimage-host.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | # Disclosed verification orchestration. The parser under test is handwritten seeded Omega |
| 4 | # (native/gsp/fwimage.om + omega-mv om/tls/sha2.om). This script only coordinates the build and |
| 5 | # compares it with independent oracles: readelf -SW (binutils), dd + sha256sum (coreutils), and |
| 6 | # L332's C tool ck_gsp_fw_inspect.c, built into a temporary directory as a verification oracle only. |
| 7 | # Read-only: no device access; the firmware file is only read; mutated copies live in a temp dir. |
| 8 | # |
| 9 | # usage: run-fwimage-host.sh /abs/path/to/pinned-omc /abs/path/to/gsp_ga10x.bin |
| 10 | # The pinned omc must sit in an omega-mv checkout (seed/out/omc), whose om/tls/sha2.om is used. |
| 11 | # CK_GSP_ROOT overrides the oracle tree (default: L332's aienos-own-gpu-driver worktree). |
| 12 | # Exit: 0 FWIMAGE PASS; 1 FAIL; 64 usage; 69 pin refused; 74 I/O; 77 NOT_RUN. |
| 13 | set -u |
| 14 | PATH=/usr/bin:/bin |
| 15 | LC_ALL=C |
| 16 | export PATH LC_ALL |
| 17 | |
| 18 | if [ "$(uname -s)" != Linux ] || [ "$(uname -m)" != aarch64 ]; then |
| 19 | echo 'NOT_RUN: fwimage host test needs Linux AArch64 and the pinned seeded omc' >&2 |
| 20 | exit 77 |
| 21 | fi |
| 22 | if [ "$#" -ne 2 ]; then |
| 23 | echo 'usage: run-fwimage-host.sh /absolute/path/to/pinned-omc /absolute/path/to/gsp_ga10x.bin' >&2 |
| 24 | exit 64 |
| 25 | fi |
| 26 | omc=$1 |
| 27 | fw=$2 |
| 28 | case $omc in /*) ;; *) echo 'usage: omc path must be absolute' >&2; exit 64 ;; esac |
| 29 | case $fw in /*) ;; *) echo 'usage: firmware path must be absolute' >&2; exit 64 ;; esac |
| 30 | |
| 31 | # Pins. omc: omega-mv 369dbc96 seed/out/omc, equal to its committed seed/out/omc.sha256. |
| 32 | # sha2.om: omega-mv 369dbc96 om/tls/sha2.om. Firmware: 580.173.02 gsp_ga10x.bin as installed. |
| 33 | omc_pin=2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f |
| 34 | sha2_pin=5cf0f98116def6917d3a373f2bd4c3932d46e3ff84d9567bbae9e720366fdb33 |
| 35 | fw_pin=32cb7cf3e77f97e92f6e92135939ccb3ba382748144569c0dfccf3338d43d963 |
| 36 | |
| 37 | digest() { d=$(sha256sum < "$1") || return 1; printf '%s\n' "${d%% *}"; } |
| 38 | |
| 39 | root=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd) || exit 74 |
| 40 | sha2=$(CDPATH= cd -- "$(dirname -- "$omc")/../.." && pwd)/om/tls/sha2.om || exit 74 |
| 41 | expected=$root/tests/gsp/expected-580.173.02.txt |
| 42 | src=$root/native/gsp/fwimage.om |
| 43 | ck_root=${CK_GSP_ROOT:-/home/drakestapleton/workspace/aienos-own-gpu-driver/native} |
| 44 | |
| 45 | [ "$(digest "$omc")" = "$omc_pin" ] || { echo "REFUSED: $omc digest differs from pin $omc_pin" >&2; exit 69; } |
| 46 | [ "$(digest "$sha2")" = "$sha2_pin" ] || { echo "REFUSED: $sha2 digest differs from pin $sha2_pin" >&2; exit 69; } |
| 47 | [ "$(digest "$fw")" = "$fw_pin" ] || { echo "REFUSED: $fw digest differs from pin $fw_pin" >&2; exit 69; } |
| 48 | echo "PIN omc $omc_pin" |
| 49 | echo "PIN sha2.om $sha2_pin" |
| 50 | echo "PIN firmware $fw_pin" |
| 51 | echo "SRC fwimage.om $(digest "$src")" |
| 52 | |
| 53 | out=$(mktemp -d "${TMPDIR:-/tmp}/cudapp-fwimage.XXXXXX") || exit 74 |
| 54 | echo "Outputs: $out" |
| 55 | fail=0 |
| 56 | bad() { echo "FAIL: $*" >&2; fail=1; } |
| 57 | |
| 58 | # 1. Compile: fwimage.om then sha2.om, one OM-2 unit. |
| 59 | cat "$src" "$sha2" > "$out/unit.om" || exit 74 |
| 60 | timeout 60 "$omc" < "$out/unit.om" > "$out/fwimage" 2> "$out/compile.err" |
| 61 | rc=$? |
| 62 | if [ "$rc" -ne 0 ] || [ -s "$out/compile.err" ] || [ ! -s "$out/fwimage" ]; then |
| 63 | cat "$out/compile.err" >&2 |
| 64 | echo "FAIL: compile exit $rc" >&2 |
| 65 | exit 1 |
| 66 | fi |
| 67 | chmod 700 "$out/fwimage" || exit 74 |
| 68 | echo "COMPILE PASS $(digest "$out/fwimage")" |
| 69 | |
| 70 | # 2. Run on the real file: no family, gb20y, gb10y. |
| 71 | for arm in none gb20y gb10y; do |
| 72 | if [ "$arm" = none ]; then set -- "$fw"; else set -- "$fw" "$arm"; fi |
| 73 | timeout 120 "$out/fwimage" "$@" > "$out/run-$arm.out" 2> "$out/run-$arm.err" |
| 74 | rc=$? |
| 75 | if [ "$rc" -ne 0 ] || [ -s "$out/run-$arm.err" ]; then |
| 76 | cat "$out/run-$arm.err" >&2 |
| 77 | bad "fwimage $arm exit $rc" |
| 78 | fi |
| 79 | done |
| 80 | |
| 81 | # 3. Reference report from readelf -SW, dd and sha256sum only (never from fwimage output). |
| 82 | sect() { # FILE -> "index name off size" rows (hex without 0x) |
| 83 | readelf -SW "$1" | sed -n 's/^ *\[ *\([0-9][0-9]*\)\] /\1 /p' | awk '$1 > 0 { print $1, $2, $5, $6 }' |
| 84 | } |
| 85 | span_sha() { # FILE OFFHEX SIZEHEX |
| 86 | dd if="$1" iflag=skip_bytes,count_bytes skip=$((0x$2)) count=$((0x$3)) bs=1048576 status=none \ |
| 87 | | sha256sum | cut -d' ' -f1 |
| 88 | } |
| 89 | gen_ref() { # FILE -> reference report, including both SELECT lines |
| 90 | sect "$1" > "$out/ref.rows" || return 1 |
| 91 | [ -s "$out/ref.rows" ] || return 1 |
| 92 | while read -r i name off size; do |
| 93 | printf 'SECTION %s off=0x%x size=0x%x\n' "$name" "0x$off" "0x$size" |
| 94 | done < "$out/ref.rows" |
| 95 | set -- "$1" $(awk '$2 == ".fwversion" { print $3, $4 }' "$out/ref.rows") |
| 96 | printf 'FWVERSION %s\n' "$(dd if="$1" iflag=skip_bytes,count_bytes skip=$((0x$2)) count=$((0x$3)) status=none | tr -d '\000')" |
| 97 | while read -r i name off size; do |
| 98 | case $name in .fwimage|.fwsignature_*) printf 'SHA256 %s %s\n' "$name" "$(span_sha "$1" "$off" "$size")" ;; esac |
| 99 | done < "$out/ref.rows" |
| 100 | set -- "$1" $(awk '$2 == ".fwimage" { print $3, $4 }' "$out/ref.rows") |
| 101 | for f in gb20y gb10y; do |
| 102 | s=$(awk -v n=".fwsignature_$f" '$2 == n { print $3, $4 }' "$out/ref.rows") |
| 103 | printf 'SELECT family=%s image_off=0x%x image_size=0x%x sig_section=.fwsignature_%s sig_sha256=%s\n' \ |
| 104 | "$f" "0x$2" "0x$3" "$f" "$(span_sha "$1" ${s})" |
| 105 | done |
| 106 | } |
| 107 | gen_ref "$fw" > "$out/reference.txt" || { echo 'FAIL: readelf/dd reference generation' >&2; exit 1; } |
| 108 | |
| 109 | # compare EXPECTED_FILE: the committed expectation must equal the fresh tool reference, and each |
| 110 | # fwimage run must equal the expectation (base lines, plus the one SELECT line for its family). |
| 111 | compare() { |
| 112 | grep -v '^#' "$1" > "$out/exp.body" || return 1 |
| 113 | cmp -s "$out/exp.body" "$out/reference.txt" || { echo " differs from readelf/dd reference: $1" >&2; diff "$out/exp.body" "$out/reference.txt" >&2; return 1; } |
| 114 | grep -v '^SELECT ' "$out/exp.body" > "$out/exp.none" |
| 115 | cmp -s "$out/exp.none" "$out/run-none.out" || { diff "$out/exp.none" "$out/run-none.out" >&2; return 1; } |
| 116 | for f in gb20y gb10y; do |
| 117 | { cat "$out/exp.none"; grep "^SELECT family=$f " "$out/exp.body"; } > "$out/exp.$f" |
| 118 | cmp -s "$out/exp.$f" "$out/run-$f.out" || { diff "$out/exp.$f" "$out/run-$f.out" >&2; return 1; } |
| 119 | done |
| 120 | return 0 |
| 121 | } |
| 122 | if compare "$expected"; then |
| 123 | echo "EXPECTED PASS $(grep -vc '^#' "$expected") lines = readelf/dd/sha256sum reference = fwimage output (3 runs)" |
| 124 | else |
| 125 | bad 'fwimage output, expected file and readelf/dd reference disagree' |
| 126 | fi |
| 127 | |
| 128 | # 4. Checker negative control: one altered digit in the expectation must make compare fail. |
| 129 | awk '/^SHA256 \.fwimage / { p = length("SHA256 .fwimage ") + 11; c = substr($0, p, 1); $0 = substr($0, 1, p - 1) (c == "0" ? "1" : "0") substr($0, p + 1) } { print }' \ |
| 130 | "$expected" > "$out/expected-mutant.txt" |
| 131 | if [ "$(cmp -l "$expected" "$out/expected-mutant.txt" | wc -l)" -ne 1 ]; then |
| 132 | bad 'checker mutant did not change exactly one byte' |
| 133 | elif compare "$out/expected-mutant.txt" 2> "$out/checker-mutant.diff"; then |
| 134 | bad 'checker accepted an altered expected line' |
| 135 | else |
| 136 | echo 'CHECKER-NEGATIVE PASS altered expected digest line is rejected' |
| 137 | fi |
| 138 | |
| 139 | # 5. C oracle (verification only): compare the lines it shares with fwimage. |
| 140 | if [ ! -r "$ck_root/kernel/tools/ck_gsp_fw_inspect.c" ] || [ ! -r "$ck_root/argus/sha256.c" ]; then |
| 141 | echo "NOT_RUN: C oracle source absent under $ck_root" >&2 |
| 142 | exit 77 |
| 143 | fi |
| 144 | echo "ORACLE source $(digest "$ck_root/kernel/tools/ck_gsp_fw_inspect.c") ck_gsp_fw_inspect.c ($(git -C "$ck_root" rev-parse --short HEAD 2>/dev/null || echo unknown))" |
| 145 | cc -std=gnu11 -O2 -Wall -Wextra -Werror -I"$ck_root/argus" -o "$out/ck_gsp_fw_inspect" \ |
| 146 | "$ck_root/kernel/tools/ck_gsp_fw_inspect.c" "$ck_root/argus/sha256.c" 2> "$out/oracle-build.err" \ |
| 147 | || { cat "$out/oracle-build.err" >&2; bad 'C oracle build'; } |
| 148 | for f in gb20y gb10y; do |
| 149 | "$out/ck_gsp_fw_inspect" inspect "$fw" --family "$f" > "$out/oracle-$f.txt" 2>&1 || bad "C oracle refused $f" |
| 150 | grep -q '^RESULT OK$' "$out/oracle-$f.txt" || bad "C oracle RESULT not OK for $f" |
| 151 | awk -v fam="$f" ' |
| 152 | $1 == "section" && $2 > 0 { split($5, o, "="); split($6, s, "="); printf "SECTION %s off=0x%x size=0x%x\n", $3, o[2], s[2]; if ($3 == ".fwimage") { io = o[2]; is = s[2] } } |
| 153 | $1 == "fwversion" { print "FWVERSION " $2 } |
| 154 | $1 == "sha256_fwimage" { print "SHA256 .fwimage " $2 } |
| 155 | $1 == "signature_section" { sig = $2 } |
| 156 | $1 == "sha256_signature" { sh = $2 } |
| 157 | END { printf "SELECT family=%s image_off=0x%x image_size=0x%x sig_section=%s sig_sha256=%s\n", fam, io, is, sig, sh } |
| 158 | ' "$out/oracle-$f.txt" > "$out/oracle-$f.lines" |
| 159 | grep -E '^(SECTION |FWVERSION |SHA256 \.fwimage |SELECT )' "$out/run-$f.out" > "$out/fwimage-$f.lines" |
| 160 | if cmp -s "$out/oracle-$f.lines" "$out/fwimage-$f.lines"; then |
| 161 | echo "ORACLE PASS $f $(wc -l < "$out/oracle-$f.lines") shared lines equal (sections, version, image digest, selection)" |
| 162 | else |
| 163 | diff "$out/oracle-$f.lines" "$out/fwimage-$f.lines" >&2 |
| 164 | bad "C oracle disagrees for $f" |
| 165 | fi |
| 166 | done |
| 167 | |
| 168 | # 6. Negative controls on mutated copies. Each must refuse with its documented code, print nothing |
| 169 | # on stdout, and the copy must differ from the original only where intended. |
| 170 | neg=$out/neg |
| 171 | mkdir "$neg" || exit 74 |
| 172 | le8() { # VALUE OFFSET FILE: write VALUE as 8 little-endian bytes at OFFSET |
| 173 | v=$1 k=0 |
| 174 | while [ "$k" -lt 8 ]; do |
| 175 | printf "\\$(printf '%03o' $((v & 255)))" |
| 176 | v=$((v >> 8)) k=$((k + 1)) |
| 177 | done | dd of="$3" bs=1 seek="$2" conv=notrunc status=none |
| 178 | } |
| 179 | poke() { # OFFSET CHAR FILE |
| 180 | printf '%s' "$2" | dd of="$3" bs=1 seek="$1" conv=notrunc status=none |
| 181 | } |
| 182 | expect_refusal() { # NAME CODE CHANGED_BYTES FILE [FAMILY] |
| 183 | name=$1 code=$2 nbytes=$3 file=$4 |
| 184 | shift 4 |
| 185 | if [ "$nbytes" != - ]; then |
| 186 | n=$(cmp -l "$fw" "$file" 2>/dev/null | wc -l) |
| 187 | [ "$n" -eq "$nbytes" ] || { bad "control $name changed $n bytes, intended $nbytes"; return; } |
| 188 | fi |
| 189 | timeout 60 "$out/fwimage" "$file" "$@" > "$neg/$name.out" 2> "$neg/$name.err" |
| 190 | rc=$? |
| 191 | if [ "$rc" -eq "$code" ] && [ ! -s "$neg/$name.out" ] && grep -q '^FWIMAGE REFUSE ' "$neg/$name.err"; then |
| 192 | echo "NEGATIVE PASS $name exit $rc: $(cat "$neg/$name.err")" |
| 193 | else |
| 194 | bad "control $name exit $rc (want $code), stdout $(wc -c < "$neg/$name.out") bytes" |
| 195 | fi |
| 196 | } |
| 197 | shoff=$(readelf -hW "$fw" | awk -F: '/Start of section headers/ { split($2, a, " "); print a[1] }') |
| 198 | fsize=$(wc -c < "$fw") |
| 199 | row() { awk -v n="$1" '$2 == n { print $1, $3, $4 }' "$out/ref.rows"; } |
| 200 | set -- $(row .shstrtab); strtab=$((0x$2)) |
| 201 | nameoff() { od -An -tu4 -j $((shoff + $1 * 64)) -N4 "$fw" | tr -d ' '; } |
| 202 | |
| 203 | head -c 1000 "$fw" > "$neg/truncated.bin" |
| 204 | expect_refusal truncated 12 - "$neg/truncated.bin" |
| 205 | |
| 206 | cp "$fw" "$neg/magic.bin" && poke 1 X "$neg/magic.bin" |
| 207 | expect_refusal magic 10 1 "$neg/magic.bin" |
| 208 | rm -f "$neg/magic.bin" |
| 209 | |
| 210 | set -- $(row .fwversion); vidx=$1 |
| 211 | cp "$fw" "$neg/offpast.bin" && le8 $((fsize + 4096)) $((shoff + vidx * 64 + 24)) "$neg/offpast.bin" |
| 212 | expect_refusal offpast 14 - "$neg/offpast.bin" |
| 213 | rm -f "$neg/offpast.bin" |
| 214 | |
| 215 | cp "$fw" "$neg/rename.bin" && poke $((strtab + $(nameoff "$vidx") + 3)) x "$neg/rename.bin" |
| 216 | expect_refusal rename-fwversion 16 1 "$neg/rename.bin" |
| 217 | rm -f "$neg/rename.bin" |
| 218 | |
| 219 | set -- $(row .fwsignature_gb10x); didx=$1 |
| 220 | cp "$fw" "$neg/dup.bin" && poke $((strtab + $(nameoff "$didx") + 17)) y "$neg/dup.bin" |
| 221 | expect_refusal duplicate-name 18 1 "$neg/dup.bin" |
| 222 | rm -f "$neg/dup.bin" |
| 223 | |
| 224 | expect_refusal unknown-family 17 0 "$fw" gb99z |
| 225 | |
| 226 | if [ "$fail" -eq 0 ]; then |
| 227 | echo 'FWIMAGE PASS' |
| 228 | exit 0 |
| 229 | fi |
| 230 | echo 'FWIMAGE FAIL' >&2 |
| 231 | exit 1 |