|
| 1 | +#!/bin/sh |
| 2 | +# shellcheck disable=SC2086 # $PROG_OPTS intentionally unquoted for word splitting |
| 3 | +# WP functional test for internal Intel SPI (opaque/hwseq) programmer. |
| 4 | +# Tests wp status, wp list, and command exit codes. |
| 5 | +# |
| 6 | +# Usage: wp-test [flashprog-programmer-opts] |
| 7 | +# |
| 8 | +# On PCH100+ (Meteor Lake etc.) coreboot pre-programs PRRs with WP=1 as |
| 9 | +# preparation for the kexec lockdown, but FLOCKDN=0 during heads execution |
| 10 | +# means that protection is not yet enforced. wp status correctly reports |
| 11 | +# 'disabled' in this case. wp enable/disable commands are tested for exit |
| 12 | +# code only; persistence across invocations requires FLOCKDN=1. |
| 13 | + |
| 14 | +PASS=0 |
| 15 | +FAIL=0 |
| 16 | +SKIP=0 |
| 17 | + |
| 18 | +SEP="========================================" |
| 19 | + |
| 20 | +pass() { echo "PASS: $1"; PASS=$(( PASS + 1 )); } |
| 21 | +fail() { echo "FAIL: $1"; FAIL=$(( FAIL + 1 )); } |
| 22 | +skip() { echo "SKIP: $1"; SKIP=$(( SKIP + 1 )); } |
| 23 | +info() { echo "INFO: $1"; } |
| 24 | + |
| 25 | +PROG_OPTS="${*}" |
| 26 | +if [ -z "$PROG_OPTS" ] && [ -n "$CONFIG_FLASH_OPTIONS" ]; then |
| 27 | + PROG_OPTS="$(echo "$CONFIG_FLASH_OPTIONS" | \ |
| 28 | + sed 's/^flashprog[[:space:]]*//' | \ |
| 29 | + sed 's/--progress[[:space:]]*//')" |
| 30 | +fi |
| 31 | +if [ -z "$PROG_OPTS" ]; then |
| 32 | + PROG_OPTS="--programmer internal" |
| 33 | +fi |
| 34 | + |
| 35 | +# Strip layout flags: 'flashprog wp' subcommands do not accept --ifd or -i. |
| 36 | +PROG_OPTS="$(echo "$PROG_OPTS" | \ |
| 37 | + sed 's/--ifd[[:space:]]*//' | \ |
| 38 | + sed 's/--image[[:space:]]*[a-z][a-z]*[[:space:]]*//g' | \ |
| 39 | + sed 's/-i[[:space:]]*[a-z][a-z]*[[:space:]]*//g' | \ |
| 40 | + sed 's/[[:space:]]*$//')" |
| 41 | + |
| 42 | + |
| 43 | +echo "$SEP" |
| 44 | +echo "flashprog WP functional test" |
| 45 | +echo "Programmer opts: $PROG_OPTS" |
| 46 | +echo "$SEP" |
| 47 | +echo "" |
| 48 | + |
| 49 | +# --- Test 1: wp status exits cleanly --- |
| 50 | +echo "--- Test 1: wp status succeeds on opaque/hwseq programmer ---" |
| 51 | +status_out="$(flashprog wp status $PROG_OPTS 2>&1)" |
| 52 | +status_rc=$? |
| 53 | +echo "$status_out" |
| 54 | +if [ "$status_rc" -eq 0 ]; then |
| 55 | + pass "wp status exit code 0 (opaque programmer WP read path works)" |
| 56 | +else |
| 57 | + fail "wp status failed (rc=$status_rc) - opaque WP dispatch not reached" |
| 58 | +fi |
| 59 | + |
| 60 | +# --- Test 2: wp status reports a recognised protection mode --- |
| 61 | +echo "" |
| 62 | +echo "--- Test 2: wp status reports a valid PCH PRR protection mode ---" |
| 63 | +if echo "$status_out" | grep -qiE 'Protection mode:'; then |
| 64 | + mode="$(echo "$status_out" | grep -i 'Protection mode:' | head -1)" |
| 65 | + pass "wp status shows '$mode'" |
| 66 | +else |
| 67 | + fail "wp status missing 'Protection mode:' field - ich_hwseq_wp_read_cfg not reached" |
| 68 | + info "Raw output: $status_out" |
| 69 | +fi |
| 70 | + |
| 71 | +# --- Test 3: wp list exits cleanly --- |
| 72 | +echo "" |
| 73 | +echo "--- Test 3: wp list succeeds on opaque/hwseq programmer ---" |
| 74 | +list_out="$(flashprog wp list $PROG_OPTS 2>&1)" |
| 75 | +list_rc=$? |
| 76 | +echo "$list_out" |
| 77 | +if [ "$list_rc" -eq 0 ]; then |
| 78 | + pass "wp list exit code 0 (ich_hwseq_wp_get_ranges reached)" |
| 79 | +else |
| 80 | + fail "wp list failed (rc=$list_rc)" |
| 81 | +fi |
| 82 | + |
| 83 | +# --- Test 4: wp list returns chip-size-derived ranges --- |
| 84 | +echo "" |
| 85 | +echo "--- Test 4: wp list returns power-of-2 top ranges derived from chip size ---" |
| 86 | +range_count="$(echo "$list_out" | grep -c 'start=0x' || true)" |
| 87 | +if [ "$range_count" -gt 2 ]; then |
| 88 | + chip_size_hex="$(echo "$list_out" | grep 'length=' | tail -1 | \ |
| 89 | + grep -oE 'length=0x[0-9a-fA-F]+' | grep -oE '0x[0-9a-fA-F]+')" |
| 90 | + pass "wp list returned $range_count ranges; chip size $chip_size_hex" |
| 91 | +else |
| 92 | + fail "wp list returned $range_count ranges (expected >2 power-of-2 entries)" |
| 93 | + info "Raw output: $list_out" |
| 94 | +fi |
| 95 | + |
| 96 | +# --- Test 5: FLOCKDN state --- |
| 97 | +echo "" |
| 98 | +echo "--- Test 5: PCH SPI FLOCKDN state ---" |
| 99 | +verbose_out="$(flashprog wp status $PROG_OPTS --verbose 2>&1)" |
| 100 | +if echo "$verbose_out" | grep -qi 'locked down\|Configuration is locked'; then |
| 101 | + info "FLOCKDN is SET - PRRs are hardware-locked, protection is enforced" |
| 102 | + flockdn_set=1 |
| 103 | +else |
| 104 | + info "FLOCKDN is NOT set - PRR protection not enforced (pre-kexec state)" |
| 105 | + info "coreboot pre-programs PRRs but locks them only at kexec time" |
| 106 | + flockdn_set=0 |
| 107 | +fi |
| 108 | +pass "FLOCKDN detection completed (see INFO above)" |
| 109 | + |
| 110 | +# --- Test 6: wp status reports correct enforcement state --- |
| 111 | +echo "" |
| 112 | +echo "--- Test 6: wp status reflects actual enforcement (FLOCKDN-aware) ---" |
| 113 | +if [ "$flockdn_set" -eq 0 ]; then |
| 114 | + # FLOCKDN=0: ich9_set_pr clears WP bits successfully, so no protection |
| 115 | + # is actually enforced regardless of what coreboot wrote to PRRs. |
| 116 | + if echo "$status_out" | grep -qi 'disabled'; then |
| 117 | + pass "wp status reports 'disabled' (FLOCKDN=0, protection not enforced)" |
| 118 | + else |
| 119 | + fail "wp status should report 'disabled' when FLOCKDN=0 (PRRs are not locked)" |
| 120 | + info "Status output: $status_out" |
| 121 | + fi |
| 122 | +else |
| 123 | + # FLOCKDN=1: ich9_set_pr cannot clear WP bits, protection is enforced. |
| 124 | + if echo "$status_out" | grep -qi 'hardware'; then |
| 125 | + pass "wp status reports 'hardware' (FLOCKDN=1, protection enforced)" |
| 126 | + else |
| 127 | + info "wp status: $status_out" |
| 128 | + pass "FLOCKDN=1 detected; wp status result noted above" |
| 129 | + fi |
| 130 | +fi |
| 131 | + |
| 132 | +# --- Tests 7+: wp command exit-code tests --- |
| 133 | +echo "" |
| 134 | +echo "--- Test 7: wp disable exits cleanly ---" |
| 135 | +dis_out="$(flashprog wp disable $PROG_OPTS 2>&1)" |
| 136 | +dis_rc=$? |
| 137 | +echo "$dis_out" |
| 138 | +if [ "$dis_rc" -eq 0 ]; then |
| 139 | + pass "wp disable returned exit code 0" |
| 140 | +elif echo "$dis_out" | grep -qi 'locked\|FLOCKDN\|Configuration is locked'; then |
| 141 | + skip "wp disable blocked by FLOCKDN (PRR registers are frozen)" |
| 142 | +else |
| 143 | + fail "wp disable failed (rc=$dis_rc)" |
| 144 | +fi |
| 145 | + |
| 146 | +echo "" |
| 147 | +echo "--- Test 8: wp range + wp enable exit cleanly ---" |
| 148 | +chip_size_hex="$(echo "$list_out" | grep 'length=' | tail -1 | \ |
| 149 | + grep -oE 'length=0x[0-9a-fA-F]+' | grep -oE '0x[0-9a-fA-F]+')" |
| 150 | +if [ -z "$chip_size_hex" ]; then |
| 151 | + skip "wp range/enable - could not determine chip size from wp list output" |
| 152 | +else |
| 153 | + chip_size=$(( chip_size_hex )) |
| 154 | + range_start=$(( chip_size - 4096 )) |
| 155 | + range_len=4096 |
| 156 | + printf "Using top 4KB range: start=0x%x len=0x%x\n" "$range_start" "$range_len" |
| 157 | + range_arg="$(printf '0x%x,0x%x' "$range_start" "$range_len")" |
| 158 | + range_out="$(flashprog wp range $PROG_OPTS "$range_arg" 2>&1)" |
| 159 | + range_rc=$? |
| 160 | + echo "$range_out" |
| 161 | + en_out="$(flashprog wp enable $PROG_OPTS 2>&1)" |
| 162 | + en_rc=$? |
| 163 | + echo "$en_out" |
| 164 | + if [ "$range_rc" -eq 0 ] && [ "$en_rc" -eq 0 ]; then |
| 165 | + pass "wp range + wp enable returned exit code 0" |
| 166 | + if [ "$flockdn_set" -eq 0 ]; then |
| 167 | + info "FLOCKDN=0: wp enable writes PRR but protection is not persistently" \ |
| 168 | + "enforced; FLOCKDN is set only at kexec time by coreboot/heads" |
| 169 | + fi |
| 170 | + elif echo "$range_out$en_out" | grep -qi 'locked\|FLOCKDN\|Configuration is locked'; then |
| 171 | + skip "wp range/enable blocked by FLOCKDN" |
| 172 | + else |
| 173 | + fail "wp range/enable failed (range_rc=$range_rc en_rc=$en_rc)" |
| 174 | + fi |
| 175 | +fi |
| 176 | + |
| 177 | +# --- Summary --- |
| 178 | +echo "" |
| 179 | +echo "$SEP" |
| 180 | +echo "Results: PASS=$PASS FAIL=$FAIL SKIP=$SKIP" |
| 181 | +echo "$SEP" |
| 182 | + |
| 183 | +[ "$FAIL" -eq 0 ] |
0 commit comments