#!/usr/bin/env bash # SPDX-License-Identifier: MIT # Checks that the FwUpdate apply paths really run from RAM only: the inflate copy # (RAMFUNC in src/helpers/FwUpdate.cpp) and the delta applier (FwPatch.cpp with # the vendored detools, moved to RAM by tools/ota_ramlib.py). EVERY function # that lives in RAM is checked: each direct branch (bl, b, b.w) must land in # RAM, and no literal may point into the flash the apply erases. GCC silently # moved the never-written inflate tables to flash once -- this is the guard for # that, and for a libc call slipping into the vendored code. # # tools/ota_check_ramfunc.sh set -euo pipefail case "${1:-}" in -h | --help | "") sed -n '3,/^set -euo/{/^set -euo/d;s/^# \{0,1\}//;p}' "$0" [[ -n "${1:-}" ]] && exit 0 || exit 2 ;; esac env="$1" elf=".pio/build/${env}/firmware.elf" bin="${HOME}/.platformio/packages/toolchain-gccarmnoneeabi/bin" [[ -f "$elf" ]] || { echo "missing $elf" exit 2 } bad=0 count=0 while read -r addr size _type name; do start=$((16#$addr)) end=$((start + 16#$size)) count=$((count + 1)) dis=$("$bin/arm-none-eabi-objdump" -d -j .data \ --start-address="$start" --stop-address="$end" "$elf") while read -r target; do if ((16#$target < 0x20000000)); then echo "FAIL $name calls flash 0x$target" bad=1 fi done < <(grep -oP '\s(bl|b|b\.w|bl\.w|b\.n)\s+\K[0-9a-f]+(?=\s+<)' <<<"$dis" || true) while read -r word; do v=$((word)) if ((v >= 0x27000 && v < 0x100000 && v != 0xFF000)); then echo "FAIL $name uses flash address $word" bad=1 fi # literal words; the halfword offsets of an inline tbb/tbh jump table that # objdump prints as .word are skipped (they are not addresses) done < <(awk '/\ttb[bh]\t/ { skip = 1; next } /\t\.(word|short|byte)\t/ { if (!skip) { sub(/.*\.word\t/, ""); if ($0 ~ /^0x/) print $1 } ; next } { skip = 0 }' <<<"$dis") done < <("$bin/arm-none-eabi-nm" -S "$elf" | awk '$3 ~ /^[tT]$/ && $1 ~ /^2000/') ram_syms=$("$bin/arm-none-eabi-nm" "$elf" | awk '$1 ~ /^2000/ {print $3}') for must in fwp_run cb_read heatshrink_decoder_poll patch_and_reset copy_and_reset inf_run; do if ! grep -qE "^(_ZL[0-9]+)?${must}([A-Za-z0-9_.]*)?$" <<<"$ram_syms"; then echo "FAIL $must is not in RAM" bad=1 fi done if ((count < 10)); then echo "FAIL only $count RAM functions found, symbol names changed?" exit 1 fi if ((bad)); then exit 1; fi echo "ok: $count RAM functions, no call or pointer into flash"