diff --git a/bin/omarchy-hw-apple-silicon-generation b/bin/omarchy-hw-apple-silicon-generation new file mode 100755 index 000000000000..bd48314d8133 --- /dev/null +++ b/bin/omarchy-hw-apple-silicon-generation @@ -0,0 +1,197 @@ +#!/bin/bash +# omarchy:summary=Identify the Apple Silicon generation from device-tree data +# omarchy:hidden=true + +# This command deliberately keeps the device-tree parser in one sourceable +# function. The executable entry point below supplies the only production path; +# tests source this file and pass a fixture path to the same function. + +omarchy_apple_silicon_generation_escape() { + local value="$1" + local index + local byte + local decimal + local hex + + OMARCHY_APPLE_SILICON_ESCAPED="" + for (( index = 0; index < ${#value}; index++ )); do + byte=${value:index:1} + printf -v decimal '%d' "'${byte}" + decimal=$((decimal & 255)) + printf -v hex '%02x' "$decimal" + OMARCHY_APPLE_SILICON_ESCAPED+="\\x${hex}" + done +} + +omarchy_apple_silicon_generation_key() { + case "$1" in + apple,t8103) OMARCHY_APPLE_SILICON_KEY="m1" ;; + apple,t6000) OMARCHY_APPLE_SILICON_KEY="m1-pro" ;; + apple,t6001) OMARCHY_APPLE_SILICON_KEY="m1-max" ;; + apple,t6002) OMARCHY_APPLE_SILICON_KEY="m1-ultra" ;; + apple,t8112) OMARCHY_APPLE_SILICON_KEY="m2" ;; + apple,t6020) OMARCHY_APPLE_SILICON_KEY="m2-pro" ;; + apple,t6021) OMARCHY_APPLE_SILICON_KEY="m2-max" ;; + apple,t6022) OMARCHY_APPLE_SILICON_KEY="m2-ultra" ;; + apple,t8122) OMARCHY_APPLE_SILICON_KEY="m3" ;; + apple,t6030) OMARCHY_APPLE_SILICON_KEY="m3-pro" ;; + apple,t6031 | apple,t6034) OMARCHY_APPLE_SILICON_KEY="m3-max" ;; + apple,t6032) OMARCHY_APPLE_SILICON_KEY="m3-ultra" ;; + apple,t8132) OMARCHY_APPLE_SILICON_KEY="m4" ;; + *) OMARCHY_APPLE_SILICON_KEY="unknown" ;; + esac +} + +omarchy_apple_silicon_generation_report() { + local source="$1" + local mode="$2" + local reason="$3" + local key="$4" + local index + + if [[ $mode == "diagnose" ]]; then + printf 'source=%s\n' "$source" >&2 + for (( index = 0; index < OMARCHY_APPLE_SILICON_RECORD_COUNT; index++ )); do + omarchy_apple_silicon_generation_escape "${OMARCHY_APPLE_SILICON_RECORDS[index]}" + printf 'record[%d]=%s\n' "$index" "$OMARCHY_APPLE_SILICON_ESCAPED" >&2 + done + printf 'key=%s\nreason=%s\n' "$key" "$reason" >&2 + elif [[ $reason != "identity-confirmed" ]]; then + printf '%s\n' "$reason" >&2 + else + printf '%s\n' "$key" + fi + + [[ $reason == "identity-confirmed" ]] +} + +omarchy_apple_silicon_generation_parse() { + ( + export LC_ALL=C + set -o pipefail + + local source="$1" + local mode="$2" + local size + local last_byte + local record + local selector_count=0 + local distinct_selector=0 + local first_selector="" + local reason="" + local key="unknown" + local fd_open=0 + local saw_empty=0 + local saw_non_ascii=0 + local saw_malformed=0 + + OMARCHY_APPLE_SILICON_RECORD_COUNT=0 + OMARCHY_APPLE_SILICON_RECORDS=() + + if [[ ! -e $source ]]; then + reason="missing-compatible" + omarchy_apple_silicon_generation_report "$source" "$mode" "$reason" "$key" + return + fi + if [[ ! -r $source || ! -f $source ]]; then + reason="unreadable-compatible" + omarchy_apple_silicon_generation_report "$source" "$mode" "$reason" "$key" + return + fi + if ! size=$(wc -c < "$source"); then + reason="unreadable-compatible" + omarchy_apple_silicon_generation_report "$source" "$mode" "$reason" "$key" + return + fi + if (( size == 0 )); then + reason="empty-compatible" + omarchy_apple_silicon_generation_report "$source" "$mode" "$reason" "$key" + return + fi + if ! last_byte=$(od -An -t x1 -j "$((size - 1))" -N 1 "$source" | tr -d '[:space:]'); then + reason="unreadable-compatible" + omarchy_apple_silicon_generation_report "$source" "$mode" "$reason" "$key" + return + fi + if [[ $last_byte != "00" ]]; then + reason="truncated-record" + omarchy_apple_silicon_generation_report "$source" "$mode" "$reason" "$key" + return + fi + if ! exec 3<"$source"; then + reason="unreadable-compatible" + omarchy_apple_silicon_generation_report "$source" "$mode" "$reason" "$key" + return + fi + fd_open=1 + + while IFS= read -r -d '' record <&3; do + OMARCHY_APPLE_SILICON_RECORDS[$((OMARCHY_APPLE_SILICON_RECORD_COUNT))]="$record" + OMARCHY_APPLE_SILICON_RECORD_COUNT=$((OMARCHY_APPLE_SILICON_RECORD_COUNT + 1)) + + if [[ -z $record ]]; then + saw_empty=1 + elif [[ $record =~ [^[:print:]] ]]; then + saw_non_ascii=1 + elif [[ $record == apple,t* ]] && [[ ! $record =~ ^apple,t[0-9]{4}$ ]]; then + saw_malformed=1 + elif [[ $record =~ ^apple,t[0-9]{4}$ ]]; then + selector_count=$((selector_count + 1)) + if (( selector_count == 1 )); then + first_selector="$record" + elif [[ $record != "$first_selector" ]]; then + distinct_selector=1 + fi + fi + done + exec 3<&- + fd_open=0 + + if (( saw_empty )); then + reason="empty-compatible" + elif (( saw_non_ascii )); then + reason="non-ascii-compatible" + elif (( saw_malformed )); then + reason="malformed-compatible" + else + if (( selector_count == 0 )); then + reason="unrecognized-compatible" + elif (( distinct_selector )); then + reason="conflicting-compatible" + elif (( selector_count > 1 )); then + reason="ambiguous-compatible" + else + omarchy_apple_silicon_generation_key "$first_selector" + key="$OMARCHY_APPLE_SILICON_KEY" + if [[ $key == "unknown" ]]; then + case "$first_selector" in + apple,t6040 | apple,t6041) reason="unconfirmed-compatible" ;; + *) reason="unrecognized-compatible" ;; + esac + else + reason="identity-confirmed" + fi + fi + fi + + if (( fd_open )); then + exec 3<&- + fi + omarchy_apple_silicon_generation_report "$source" "$mode" "$reason" "$key" + ) +} + +if [[ ${BASH_SOURCE[0]} == "$0" ]]; then + if (( $# != 1 )); then + printf 'usage: %s --key|--diagnose\n' "${0##*/}" >&2 + exit 2 + fi + case "${1:-}" in + --key) omarchy_apple_silicon_generation_parse "/proc/device-tree/compatible" "key" ;; + --diagnose) omarchy_apple_silicon_generation_parse "/proc/device-tree/compatible" "diagnose" ;; + *) + printf 'usage: %s --key|--diagnose\n' "${0##*/}" >&2 + exit 2 + ;; + esac +fi diff --git a/test/shell.d/apple-silicon-generation-test.sh b/test/shell.d/apple-silicon-generation-test.sh new file mode 100644 index 000000000000..8b88acbd0d29 --- /dev/null +++ b/test/shell.d/apple-silicon-generation-test.sh @@ -0,0 +1,229 @@ +#!/bin/bash + +set -euo pipefail + +test_dir=$(cd -- "$(dirname -- "$0")" && pwd) +source "$test_dir/base-test.sh" + +authority="$ROOT/bin/omarchy-hw-apple-silicon-generation" +parser_bash="${PARSER_BASH:-/bin/bash}" +[[ -x $authority ]] || fail "Apple Silicon generation authority is executable" +pass "Apple Silicon generation authority is executable" + +header=$(awk ' + NR == 1 && /^#!/ { next } + /^[[:space:]]*$/ { if (seen) print; next } + /^[[:space:]]*#/ { seen=1; print; next } + { exit } +' "$authority") +grep -Fqx '# omarchy:summary=Identify the Apple Silicon generation from device-tree data' <<<"$header" || + fail "generation authority has command metadata" +grep -Fqx '# omarchy:hidden=true' <<<"$header" || fail "generation authority is hidden" +pass "generation authority has hidden command metadata" + +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT +fixture="$test_tmp/compatible" +stderr_file="$test_tmp/stderr" + +run_parser() { + local mode="$1" expected_status="$2" expected_output="$3" expected_reason="$4" + local source_path="${5:-$fixture}" + local actual_output actual_status + if actual_output=$("$parser_bash" -c 'source "$1"; omarchy_apple_silicon_generation_parse "$2" "$3"' bash "$authority" "$source_path" "$mode" 2>"$stderr_file"); then + actual_status=0 + else + actual_status=$? + fi + [[ $actual_status == "$expected_status" ]] || fail "$expected_reason status" "expected $expected_status, got $actual_status" + [[ $actual_output == "$expected_output" ]] || fail "$expected_reason stdout" "expected <$expected_output>, got <$actual_output>" + if [[ $mode == "key" ]]; then + if (( expected_status == 0 )); then + [[ ! -s $stderr_file ]] || fail "$expected_reason key mode is quiet" "$(cat "$stderr_file")" + else + grep -Fqx "$expected_reason" "$stderr_file" || fail "$expected_reason key mode reports reason" "$(cat "$stderr_file")" + fi + fi +} + +set_fixture() { + printf '%b' "$1" >"$fixture" +} + +confirmed_cases='m1|apple,t8103 +m1-pro|apple,t6000 +m1-max|apple,t6001 +m1-ultra|apple,t6002 +m2|apple,t8112 +m2-pro|apple,t6020 +m2-max|apple,t6021 +m2-ultra|apple,t6022 +m3|apple,t8122 +m3-pro|apple,t6030 +m3-ultra|apple,t6032 +m4|apple,t8132' +while IFS='|' read -r expected token; do + [[ -n $expected ]] || continue + set_fixture "apple,j274\\0$token\\0apple,arm-platform\\0" + run_parser key 0 "$expected" identity-confirmed + pass "confirmed $token maps to $expected" +done <<<"$confirmed_cases" + +for token in apple,t6031 apple,t6034; do + set_fixture "apple,j514\\0$token\\0" + run_parser key 0 m3-max identity-confirmed + pass "confirmed M3 Max token $token maps to m3-max" +done + +set_fixture 'apple,j274\0apple,arm-platform\0' +run_parser key 1 '' unrecognized-compatible +set_fixture 'arm64\0brcm,foo\0apple,arm-platform\0' +run_parser key 1 '' unrecognized-compatible +set_fixture 'apple,t6040\0' +run_parser key 1 '' unconfirmed-compatible +set_fixture 'apple,t6041\0' +run_parser key 1 '' unconfirmed-compatible +set_fixture 'apple,t9999\0' +run_parser key 1 '' unrecognized-compatible +pass "context, unconfirmed, and unknown selectors fail closed" + +set_fixture 'apple,t8103\0apple,t8103\0' +run_parser key 1 '' ambiguous-compatible +set_fixture 'apple,t8103\0apple,t8122\0' +run_parser key 1 '' conflicting-compatible +set_fixture 'apple,t8122\0apple,t8103\0' +run_parser key 1 '' conflicting-compatible +set_fixture 'apple,t6031\0apple,t6034\0' +run_parser key 1 '' conflicting-compatible +set_fixture 'apple,t8103\0apple,t9999\0' +run_parser key 1 '' conflicting-compatible +set_fixture 'apple,t8103\0apple,t6040\0' +run_parser key 1 '' conflicting-compatible +pass "selector multiplicity is order-independent" + +set_fixture '' +run_parser key 1 '' empty-compatible +set_fixture '\0' +run_parser key 1 '' empty-compatible +set_fixture 'apple,t8103\0\0' +run_parser key 1 '' empty-compatible +set_fixture 'apple,t8103' +run_parser key 1 '' truncated-record +set_fixture 'apple,t8103\377\0' +run_parser key 1 '' non-ascii-compatible +set_fixture 'apple,t8103\303\251\0' +run_parser key 1 '' non-ascii-compatible +set_fixture 'apple,t810x\0' +run_parser key 1 '' malformed-compatible +set_fixture 'apple,t81030\0' +run_parser key 1 '' malformed-compatible +set_fixture 'apple,t810x\0apple,t8103\377\0' +run_parser key 1 '' non-ascii-compatible +set_fixture 'apple,t8103\377\0apple,t810x\0' +run_parser key 1 '' non-ascii-compatible +set_fixture 'apple,t810x\0\0apple,t8103\377\0' +run_parser key 1 '' empty-compatible +pass "byte and grammar precedence is deterministic" + +missing="$test_tmp/missing" +if "$parser_bash" -c 'source "$1"; omarchy_apple_silicon_generation_parse "$2" key' bash "$authority" "$missing" >"$test_tmp/missing-out" 2>"$stderr_file"; then + fail "missing compatible path is rejected" +fi +[[ ! -s $test_tmp/missing-out ]] || fail "missing path has no key output" +grep -Fqx missing-compatible "$stderr_file" || fail "missing path reports its reason" +mkdir "$test_tmp/directory" +run_parser key 1 '' unreadable-compatible "$test_tmp/directory" +pass "missing and unreadable sources fail closed" + +set_fixture 'apple,t8103\0' +if diagnose=$("$parser_bash" -c 'source "$1"; omarchy_apple_silicon_generation_parse "$2" diagnose' bash "$authority" "$fixture" 2>"$stderr_file"); then + : +else + fail "diagnose accepts a confirmed fixture" "$(cat "$stderr_file")" +fi +[[ -z $diagnose ]] || fail "diagnose has no stdout" +grep -Fqx "source=$fixture" "$stderr_file" || fail "diagnose reports source" +grep -Fqx 'record[0]=\x61\x70\x70\x6c\x65\x2c\x74\x38\x31\x30\x33' "$stderr_file" || fail "diagnose escapes every record byte" +! grep -Fq 'apple,t8103' "$stderr_file" || fail "diagnose never echoes raw records" +grep -Fqx 'key=m1' <(grep '^key=' "$stderr_file") || fail "diagnose reports derived key" +grep -Fqx 'reason=identity-confirmed' <(grep '^reason=' "$stderr_file") || fail "diagnose reports identity reason" +pass "diagnose uses canonical escaped stderr output" + +set_fixture 'apple,t8103\377\0' +if diagnose=$($parser_bash -c 'source "$1"; omarchy_apple_silicon_generation_parse "$2" diagnose' bash "$authority" "$fixture" 2>"$stderr_file"); then + fail "diagnose rejects a non-ASCII record" +fi +[[ -z $diagnose ]] || fail "failed diagnose has no stdout" +grep -Fqx 'record[0]=\x61\x70\x70\x6c\x65\x2c\x74\x38\x31\x30\x33\xff' "$stderr_file" || fail "diagnose escapes invalid bytes" +! grep -Fq 'apple,t8103' "$stderr_file" || fail "failed diagnose never echoes raw records" +grep -Fqx 'reason=non-ascii-compatible' <(grep '^reason=' "$stderr_file") || fail "diagnose reports non-ASCII reason" +pass "diagnose escapes rejected bytes without raw output" + +set_fixture 'apple,t8103\0' +if direct=$("$parser_bash" -c '"$1" --key' bash "$authority" 2>"$test_tmp/direct-err"); then + direct_status=0 +else + direct_status=$? +fi +if overridden=$(OMARCHY_DEVICE_TREE_COMPATIBLE="$fixture" "$parser_bash" -c '"$1" --key' bash "$authority" 2>"$test_tmp/override-err"); then + override_status=0 +else + override_status=$? +fi +[[ $direct_status == "$override_status" ]] || fail "executable ignores source environment override" +cmp -s "$test_tmp/direct-err" "$test_tmp/override-err" || fail "executable uses only hardcoded source" +[[ $direct == "$overridden" ]] || fail "executable ignores source environment output override" +if "$authority" --key "$fixture" >"$test_tmp/extra-out" 2>"$test_tmp/extra-err"; then + extra_status=0 +else + extra_status=$? +fi +[[ $extra_status == 2 ]] || fail "executable rejects a source path argument" "expected 2, got $extra_status" +grep -Fq 'usage:' "$test_tmp/extra-err" || fail "extra source argument reports usage" +pass "executable hardcodes default source and rejects overrides" + +source_sentinel=$("$parser_bash" -c 'source "$1"; printf sourced' bash "$authority") +[[ $source_sentinel == sourced ]] || fail "authority source guard avoids running main" +pass "authority is sourceable without running main" + +stub_bin="$test_tmp/stub-bin" +calls="$test_tmp/calls" +mkdir -p "$stub_bin" +: >"$calls" +for command_name in sudo systemctl pacman mount cryptsetup git makepkg curl install; do + cat >"$stub_bin/$command_name" <<'SH' +#!/bin/bash +printf '%s\n' "${0##*/}" >>"$TEST_CALLS" +exit 99 +SH +done +chmod +x "$stub_bin"/* +set_fixture 'apple,t810x\0' +if PATH="$stub_bin:$PATH" TEST_CALLS="$calls" "$parser_bash" -c 'source "$1"; omarchy_apple_silicon_generation_parse "$2" key' bash "$authority" "$fixture" >/dev/null 2>"$stderr_file"; then + fail "rejected input remains a failure with privileged stubs" +fi +[[ ! -s $calls ]] || fail "identity parser invokes no privileged commands" "$(cat "$calls")" +pass "rejected input invokes no privileged commands" + +set_fixture 'apple,t8103\0' +mutant="$test_tmp/mutant" +cp "$authority" "$mutant" +sed -i.bak 's/apple,t8103) OMARCHY_APPLE_SILICON_KEY="m1"/apple,t8103) OMARCHY_APPLE_SILICON_KEY="unknown"/' "$mutant" +if "$parser_bash" -c 'source "$1"; omarchy_apple_silicon_generation_parse "$2" key' bash "$mutant" "$fixture" >"$test_tmp/mutant-out" 2>"$stderr_file"; then + fail "mapping mutation is detected by runtime fixture" +fi +[[ ! -s $test_tmp/mutant-out ]] || fail "mapping mutation does not produce the expected key" +pass "runtime mapping mutation fails the focused proof" + +metadata_bash=$(command -v bash) +if [[ -x /opt/homebrew/bin/bash ]]; then + metadata_bash=/opt/homebrew/bin/bash +fi +if command -v jq >/dev/null && "$metadata_bash" -c '((BASH_VERSINFO[0] >= 4))'; then + metadata=$($metadata_bash "$ROOT/bin/omarchy" commands --all --json) + jq -e --arg binary omarchy-hw-apple-silicon-generation 'any(.commands[]; .binary == $binary and .hidden == true and (.summary | length > 0))' <<<"$metadata" >/dev/null || + fail "metadata router exposes hidden generation authority" + pass "metadata router exposes hidden generation authority" +else + pass "metadata router check deferred on Bash without associative arrays or jq" +fi