reform

MNT Reform: Open Source Portable Computer
Log (Feed) | Files | Refs (Tags) | README

commit aab54cc27ef9b22a6f6f0a7a5827e956cd40f3d8
parent 857c82eaea125ba4eda54da88f288fbd3b08bb54
Author: minute <lukas@mntre.com>
Date:   Mon, 19 Feb 2024 19:09:05 +0000

Merge branch 'lpc-fw-flash.sh' into 'master'

reform2-lpc-fw/flash.sh: make script interactive

See merge request reform/reform!45
Diffstat:
Mreform2-lpc-fw/flash.sh | 81++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 74 insertions(+), 7 deletions(-)

diff --git a/reform2-lpc-fw/flash.sh b/reform2-lpc-fw/flash.sh @@ -1,11 +1,78 @@ -#!/bin/bash +#!/bin/sh -set -e +set -eu -# replace the x with the drive that LPC actually shows up as (check dmesg -w when plugging in) -# -#mount /dev/sdx /mnt -dd if=bin/firmware.bin of="/mnt/firmware.bin" conv=nocreat,notrunc +if [ ! -e ./bin/firmware.bin ]; then + echo "./bin/firmware.bin doesn't exist" >&2 + exit 1 +fi + +if [ "$(id -u)" -ne 0 ]; then + echo "you need to run this as root (for example by using sudo)" >&2 + exit 1 +fi + +disk="/dev/disk/by-id/usb-NXP_LPC1XXX_IFLASH_ISP-0:0" + +get_disk_property() { + property="$1" + if [ "$(udevadm --version)" -lt 250 ]; then + # no udevadm --property before version 250 + udevadm info --query=property "$disk" | sed -ne "s/^$property=//p" + else + udevadm info --query=property --property="$property" --value "$disk" + fi +} + +if [ ! -e "$disk" ]; then + echo 'Make sure that the battery is disconnected.' >&2 + echo 'Connect power to the mainboard via the barrel jack connector.' >&2 + echo 'Set the DIP switch LPCPROG to “ON”.' >&2 + echo 'Press the button LPCRESET.' >&2 + echo 'Connect the USB cable.' >&2 +fi + +if [ "$(udevadm --version)" -lt 251 ]; then + echo 'Press the Enter key once you are ready' >&2 + # shellcheck disable=SC2034 + read -r enter +else + echo 'Waiting for the disk to appear...' >&2 + udevadm wait --settle "$disk" +fi + +if [ "$(get_disk_property "ID_MODEL_ID")" != "000b" ]; then + echo "unexpected model id" >&2 + exit 1 +fi +if [ "$(get_disk_property "ID_VENDOR_ID")" != "1fc9" ]; then + echo "unexpected vendor id" >&2 + exit 1 +fi +if [ "$(get_disk_property "ID_MODEL")" != "LPC1XXX_IFLASH" ]; then + echo "unexpected model" >&2 + exit 1 +fi +if [ "$(get_disk_property "ID_VENDOR")" != "NXP" ]; then + echo "unexpected vendor" >&2 + exit 1 +fi + +MOUNTPOINT="$(mktemp --tmpdir --directory reform-lpc-fw.XXXXXXXXXX)" +trap 'umount $MOUNTPOINT' EXIT INT TERM +mount "$disk" "$MOUNTPOINT" + +dd if=bin/firmware.bin of="$MOUNTPOINT/firmware.bin" conv=nocreat,notrunc sync -umount "/mnt" +umount "$MOUNTPOINT" +trap - EXIT INT TERM + +echo "Firmware successfully flashed." >&2 +echo "Unplug the Micro—USB cable." >&2 + +if [ "$(udevadm --version)" -ge 251 ]; then + udevadm wait --removed "$disk" +fi +echo 'Set the DIP switch LPCPROG to “OFF”.' >&2 +echo 'Press the button LPCRESET.' >&2