Last active
September 22, 2024 06:05
-
-
Save naoki-mizuno/1518c8dbb3b1822fdd109fa2d87bada9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
TIMEOUT=9 | |
while getopts "ht:" flag; do | |
case $flag in | |
t) | |
TIMEOUT="$OPTARG" | |
;; | |
h) | |
echo "Usage: $0 [-h] [-t TIMEOUT]" | |
exit 0 | |
;; | |
esac | |
done | |
shift $(( OPTIND - 1 )) | |
BIN_FILE="$1" | |
PAD_BIN_FILE="true" | |
BEFORE="$( ls -1 /dev/sd? )" | |
if [[ -z $BIN_FILE ]]; then | |
echo "Bin file path not provided" 2>&1 | |
exit 1 | |
elif [[ ! -f $BIN_FILE ]]; then | |
echo "Hmm... can't find firmware file at $BIN_FILE" 2>&1 | |
exit 1 | |
fi | |
# Pad file to make it exactly 26 KB | |
if [[ $PAD_BIN_FILE == "true" ]]; then | |
TARGET_SIZE=26624 | |
ORIG_SIZE="$( du -b $BIN_FILE | awk '{ print $1 }' )" | |
echo "Original file size is $ORIG_SIZE bytes" | |
if [[ $TARGET_SIZE == $ORIG_SIZE ]]; then | |
echo "No need to pad" | |
else | |
# Create backup file just in case | |
cp $BIN_FILE $BIN_FILE.orig | |
dd if=/dev/null of=$BIN_FILE bs=1 count=1 seek=26624 2>/dev/null | |
# Note: Another way to do this | |
# truncate -s 26K $BIN_FILE | |
echo "Padded $BIN_FILE to 26624 bytes" | |
fi | |
fi | |
echo "You may be prompted for your password so this script can execute sudo" | |
sudo echo "OK! You can now plug in your keyboard in flash mode (plug in USB while holding ESC)" || exit 0 | |
while true; do | |
AFTER="$( ls -1 /dev/sd? )" | |
PLUGGED_IN="$( diff <( echo "$BEFORE" | sort ) <( echo "$AFTER" | sort ) | awk '/^> /{ print $2 }' )" | |
if [[ -n $PLUGGED_IN ]]; then | |
break | |
fi | |
sleep 0.5 | |
done | |
echo "Detected new device at $PLUGGED_IN" | |
echo "If you would like to abort, disconnect the device" | |
for (( t=$TIMEOUT; t > 0; t-- )); do | |
printf "Flashing $PLUGGED_IN in %-3d\r" $t | |
sleep 1 | |
done | |
if [[ ! -e $PLUGGED_IN ]]; then | |
echo "$PLUGGED_IN seemed to have disappeared. Aborting" 2>&1 | |
exit 1 | |
fi | |
echo -e "\nFlashing!" | |
sudo dd if="$BIN_FILE" of="$PLUGGED_IN" seek=4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use this script to flash your firmware binary file to your keyboard. I use yang's controller (v2.5) for my HHKB Pro2.
flash-keyboard /path/to/bin/file
dd
command