#!/bin/bash # Script builds, signs, and/or flashes Nordic board with optional debug or production overlays # Run this in `nordic/trezor` to sign and mergehex final image with mcuboot # This charade serves to differentiate commands run under poetry shell and ncs shell since their pythons are not compatible # Update the OPTSTRING to include 'a:' OPTSTRING=":b:a:pdsfc" APP_DIR="trezor-ble" BOARD= SIGN=0 FLASH=0 PRISTINE= DEBUG= PRODUCTION= fatal() { echo "$@" exit 1 } run_under_ncs_subshell() { # In the subshell, toolchain environment is sourced then the command is run (source <(nrfutil toolchain-manager env | perl -pe 's/^(\w+)\s*:\s*(.*)/export \1=\2/'); bash -x -c "$@") \ || fatal "Error in subshell" } usage() { echo "$0 [-b board_name] [-a app_dir] [-p] [-d] [-r] [-s] [-f]" cat <&2 exit 1 fi # Read version components local major=$(grep "VERSION_MAJOR" "$version_file" | cut -d'=' -f2 | tr -d ' ') local minor=$(grep "VERSION_MINOR" "$version_file" | cut -d'=' -f2 | tr -d ' ') local patch=$(grep "PATCHLEVEL" "$version_file" | cut -d'=' -f2 | tr -d ' ') local tweak=$(grep "VERSION_TWEAK" "$version_file" | cut -d'=' -f2 | tr -d ' ') # Format version string as major.minor.patch+tweak local version="$major.$minor.$patch+$tweak" echo "$version" } VERSION=$(get_version_from_file) # Update paths in signing and flashing commands if [ "$SIGN" -eq 1 ]; then run_under_ncs_subshell \ "imgtool sign --version $VERSION --align 4 --header-size 0x200 -S 0x6c000 --pad-header build/$APP_DIR/zephyr/zephyr.bin build/$APP_DIR/zephyr/zephyr.prep.bin --custom-tlv 0x00A2 0x03 && \ imgtool sign --version $VERSION --align 4 --header-size 0x200 -S 0x6c000 --pad-header build/$APP_DIR/zephyr/zephyr.hex build/$APP_DIR/zephyr/zephyr.prep.hex --custom-tlv 0x00A2 0x03 && \ ../bootloader/mcuboot/scripts/imgtool.py dumpinfo ./build/$APP_DIR/zephyr/zephyr.prep.bin > ./build/$APP_DIR/zephyr/dump.txt" HASH=$(python ./scripts/extract_hash.py ./build/$APP_DIR/zephyr/dump.txt) SIGNATURE0=$(hash_signer -d "$HASH" -s0) SIGNATURE1=$(hash_signer -d "$HASH" -s1) echo "Signed hash $HASH, signature0 $SIGNATURE0, signature1 $SIGNATURE1" run_under_ncs_subshell \ "python ./scripts/insert_signatures.py ./build/$APP_DIR/zephyr/zephyr.prep.hex $SIGNATURE0 $SIGNATURE1 -o ./build/$APP_DIR/zephyr/zephyr.signed_trz.hex && \ python ./scripts/insert_signatures.py ./build/$APP_DIR/zephyr/zephyr.prep.bin $SIGNATURE0 $SIGNATURE1 -o ./build/$APP_DIR/zephyr/zephyr.signed_trz.bin && \ python ../zephyr/scripts/build/mergehex.py build/mcuboot/zephyr/zephyr.hex build/$APP_DIR/zephyr/zephyr.signed_trz.hex -o build/zephyr.merged.signed_trz.hex" fi if [ "$FLASH" -eq 1 ]; then run_under_ncs_subshell \ 'west flash --hex-file ./build/zephyr.merged.signed_trz.hex' fi