flash.sh (2117B)
1 #!/bin/sh 2 3 set -eu 4 5 if [ ! -e ./bin/firmware.bin ]; then 6 echo "./bin/firmware.bin doesn't exist. you can compile it from source or" >&2 7 echo "use ./download-fw.sh to download the latest binary from source.mnt.re." >&2 8 exit 1 9 fi 10 11 if [ "$(id -u)" -ne 0 ]; then 12 echo "you need to run this as root (for example by using sudo)" >&2 13 exit 1 14 fi 15 16 disk="/dev/disk/by-id/usb-NXP_LPC1XXX_IFLASH_ISP-0:0" 17 18 get_disk_property() { 19 property="$1" 20 if [ "$(udevadm --version)" -lt 250 ]; then 21 # no udevadm --property before version 250 22 udevadm info --query=property "$disk" | sed -ne "s/^$property=//p" 23 else 24 udevadm info --query=property --property="$property" --value "$disk" 25 fi 26 } 27 28 if [ ! -e "$disk" ]; then 29 echo 'Make sure that the battery is disconnected.' >&2 30 echo 'Connect power to the mainboard via the barrel jack connector.' >&2 31 echo 'Set the DIP switch LPCPROG to “ON”.' >&2 32 echo 'Press the button LPCRESET.' >&2 33 echo 'Connect the USB cable.' >&2 34 fi 35 36 if [ "$(udevadm --version)" -lt 251 ]; then 37 echo 'Press the Enter key once you are ready' >&2 38 # shellcheck disable=SC2034 39 read -r enter 40 else 41 echo 'Waiting for the disk to appear...' >&2 42 udevadm wait --settle "$disk" 43 fi 44 45 if [ "$(get_disk_property "ID_MODEL_ID")" != "000b" ]; then 46 echo "unexpected model id" >&2 47 exit 1 48 fi 49 if [ "$(get_disk_property "ID_VENDOR_ID")" != "1fc9" ]; then 50 echo "unexpected vendor id" >&2 51 exit 1 52 fi 53 if [ "$(get_disk_property "ID_MODEL")" != "LPC1XXX_IFLASH" ]; then 54 echo "unexpected model" >&2 55 exit 1 56 fi 57 if [ "$(get_disk_property "ID_VENDOR")" != "NXP" ]; then 58 echo "unexpected vendor" >&2 59 exit 1 60 fi 61 62 MOUNTPOINT="$(mktemp --tmpdir --directory reform-lpc-fw.XXXXXXXXXX)" 63 trap 'umount $MOUNTPOINT' EXIT INT TERM 64 mount "$disk" "$MOUNTPOINT" 65 66 dd if=bin/firmware.bin of="$MOUNTPOINT/firmware.bin" conv=nocreat,notrunc 67 sync 68 umount "$MOUNTPOINT" 69 trap - EXIT INT TERM 70 71 echo "Firmware successfully flashed." >&2 72 echo "Unplug the Micro—USB cable." >&2 73 74 if [ "$(udevadm --version)" -ge 251 ]; then 75 udevadm wait --removed "$disk" 76 fi 77 78 echo 'Set the DIP switch LPCPROG to “OFF”.' >&2 79 echo 'Press the button LPCRESET.' >&2