|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +if [ ! -f ".config" ]; then |
| 5 | + echo "Missing .config. Run make config first." >&2 |
| 6 | + exit 1 |
| 7 | +fi |
| 8 | + |
| 9 | +if ! grep -Eq '^(DUALBANK_SWAP(\?|)=1)' .config; then |
| 10 | + echo "DUALBANK_SWAP=1 is required for this simulation." >&2 |
| 11 | + exit 1 |
| 12 | +fi |
| 13 | + |
| 14 | +if [ ! -x "./wolfboot.elf" ]; then |
| 15 | + echo "wolfboot.elf not found. Build the simulator first." >&2 |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +if [ ! -f "./internal_flash.dd" ]; then |
| 20 | + echo "internal_flash.dd not found. Build test-sim-internal-flash-with-update first." >&2 |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +backup_image="$(mktemp ./internal_flash.rollback.XXXXXX)" |
| 25 | +cp ./internal_flash.dd "$backup_image" |
| 26 | +trap 'cp "$backup_image" ./internal_flash.dd; rm -f "$backup_image" sim_registers.dd' EXIT |
| 27 | + |
| 28 | +rm -f sim_registers.dd |
| 29 | + |
| 30 | +update_addr_hex="$(grep '^WOLFBOOT_PARTITION_UPDATE_ADDRESS=' .config | cut -d= -f2)" |
| 31 | +if [ -z "${update_addr_hex}" ]; then |
| 32 | + echo "WOLFBOOT_PARTITION_UPDATE_ADDRESS is not set in .config." >&2 |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | + |
| 36 | +update_addr=$((update_addr_hex)) |
| 37 | + |
| 38 | +# Corrupt UPDATE payload bytes so version metadata remains intact but |
| 39 | +# image verification fails and boot logic attempts fallback. |
| 40 | +printf '\x00\x00\x00\x00\x00\x00\x00\x00' | \ |
| 41 | + dd of=./internal_flash.dd bs=1 seek="$((update_addr + 0x120))" conv=notrunc status=none |
| 42 | + |
| 43 | +set +e |
| 44 | +rollback_output="$(timeout 3s ./wolfboot.elf get_version 2>&1)" |
| 45 | +rollback_rc=$? |
| 46 | +set -e |
| 47 | + |
| 48 | +if [ "$rollback_rc" -eq 0 ]; then |
| 49 | + echo "Expected rollback denial, but boot continued normally." >&2 |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +if [ "$rollback_rc" -ne 124 ] && [ "$rollback_rc" -ne 80 ]; then |
| 54 | + echo "Unexpected exit code while checking rollback denial: $rollback_rc" >&2 |
| 55 | + echo "$rollback_output" >&2 |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | + |
| 59 | +if ! printf '%s\n' "$rollback_output" | grep -q "Rollback to lower version not allowed"; then |
| 60 | + echo "Rollback denial message not found in output." >&2 |
| 61 | + echo "$rollback_output" >&2 |
| 62 | + exit 1 |
| 63 | +fi |
| 64 | + |
| 65 | +echo "Dualbank rollback-to-older-version denial verified." |
0 commit comments