Skip to content

Commit d7cc386

Browse files
committed
Normalize NS350 chip state at entry/exit of nations SPDM tests
NS350's Nations_IdentityKeySet returns TPM_RC_VALUE when asked to set the identity key to its current value. The nations test assumed the chip always started at identity-key=1, but self-hosted CI runners carry persistent NV state across runs — any prior failure left the chip in a state the next run could not recover from. GPIO reset clears volatile state but does not reset NV-persistent identity-key or PSK provisioning. Add normalize_nations_chip(): GPIO reset + idempotent --psk-clear + idempotent --identity-key-set. Call at entry of both nations and nations-psk blocks, and wire to trap EXIT so the chip is always cleaned up on success, failure, or set -e early exit. Validated on Pi hardware across five scenarios (identity-key=1, identity-key=0, PSK-provisioned, nations-psk clean, nations-psk PSK-stuck): all runs now pass and leave the chip at canonical identity-key=1.
1 parent 23b3ed6 commit d7cc386

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

examples/spdm/spdm_test.sh

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ gpio_reset() {
4848
sleep 2
4949
}
5050

51+
# normalize_nations_chip: bring NS350 to canonical clean state
52+
# (identity-key=1, no PSK). Idempotent — safe to call multiple times.
53+
# NS350 IdentityKeySet returns TPM_RC_VALUE when setting to current value,
54+
# so "already in target state" is indistinguishable from real errors; we
55+
# probe by trying both transitions rather than trusting a single call.
56+
normalize_nations_chip() {
57+
echo "--- Normalizing NS350 to clean state (identity-key=1, no PSK) ---"
58+
gpio_reset
59+
# Clear PSK if set. PSKNotSet (0xffA3) means already clean — that's fine.
60+
# Any other failure is also non-fatal here; the identity-key-set below
61+
# will surface the real problem if state is unrecoverable.
62+
"$SPDM_DEMO" --psk-clear "$NATIONS_CLEARAUTH" >/dev/null 2>&1 || true
63+
# Now try to set identity key. Succeeds if at 0, benign-fails with
64+
# TPM_RC_VALUE if already at 1. Either outcome = state is 1.
65+
"$SPDM_DEMO" --identity-key-set >/dev/null 2>&1 || true
66+
echo "--- Normalization complete ---"
67+
echo ""
68+
}
69+
5170
run_test() {
5271
local name="$1"; shift
5372
TOTAL=$((TOTAL + 1))
@@ -132,7 +151,12 @@ if [ "$VENDOR" = "nuvoton" ]; then
132151

133152
elif [ "$VENDOR" = "nations" ]; then
134153
# Nations NS350 identity key mode — full lifecycle test
135-
# Note: GPIO 4 is NOT wired to TPM_RST on NS350 daughter boards.
154+
# GPIO 4 is wired to TPM_RST on NS350 and clears volatile state, but
155+
# identity-key/PSK are NV-persistent across reset. The entry/exit
156+
# normalization ensures the chip is always at a known starting state
157+
# and always left clean, regardless of prior runs or mid-test failures.
158+
normalize_nations_chip
159+
trap 'normalize_nations_chip' EXIT
136160

137161
run_test_no_reset "Unset identity key" "$SPDM_DEMO" --identity-key-unset
138162
run_test_no_reset "Set identity key" "$SPDM_DEMO" --identity-key-set
@@ -156,8 +180,10 @@ elif [ "$VENDOR" = "nations-psk" ]; then
156180
# Uses NSING reference test data (PSK_DEMO_3 from Vision's traces).
157181
# ClearAuth is always exactly 32 bytes per TCG spec.
158182

159-
# Note: GPIO 4 is NOT wired to TPM_RST on NS350 daughter boards.
160-
# Use run_test_no_reset instead of run_test.
183+
# Entry/exit normalization: always start clean (identity-key=1, no PSK)
184+
# and always end clean, regardless of prior state or mid-test failures.
185+
normalize_nations_chip
186+
trap 'normalize_nations_chip' EXIT
161187

162188
# Step 1: Ensure identity key is unset (required for PSK mode)
163189
run_test_no_reset "Unset identity key" "$SPDM_DEMO" --identity-key-unset

0 commit comments

Comments
 (0)