forked from ct-Open-Source/tuya-convert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firmware_picker.sh
executable file
·80 lines (73 loc) · 2.3 KB
/
firmware_picker.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
MAGIC=$(printf "\xe9")
while true; do
echo
echo "Available options:"
echo " 0) return to stock"
index=0
for file in ../files/*.bin; do
# skip null glob
[[ -e $file ]] || continue
# get short name
filename=$(basename "$file")
# skip files too large or too small
filesize=$(stat -Lc%s "$file")
[[ "$filesize" -gt 0x1000 && "$filesize" -le 0x80000 ]] || continue
# skip files without magic byte
[[ $(head -c 1 "$file") == "$MAGIC" ]] || continue
echo " $((++index))) flash $filename"
options[$index]="$filename"
# only show first 9 options, accessible with a single keypress
if (( index == 9 )); then
break
fi
done
echo " q) quit; do nothing"
echo -n "Please select 0-$index: "
while true; do
read -n 1 -r
echo
if [[ "$REPLY" =~ ^[0-9]$ && "$REPLY" -ge 0 && "$REPLY" -le $index ]]; then
break
fi
if [[ "$REPLY" =~ ^[Qq]$ ]]; then
echo "Leaving device as is..."
exit
fi
echo -n "Invalid selection, please select 0-$index: "
done
if [[ "$REPLY" == 0 ]]; then
if curl -s http://10.42.42.42/undo; then
echo "Disconnect the device to prevent it from repeating the upgrade"
echo "You will need to put the device back into pairing mode and register to use again"
else
echo "Could not reach the device!"
fi
break
fi
selection="${options[$REPLY]}"
read -p "Are you sure you want to flash $selection? This is the point of no return [y/N] " -n 1 -r
echo
[[ "$REPLY" =~ ^[Yy]$ ]] || continue
echo "Attempting to flash $selection, this may take a few seconds..."
RESULT=$(curl -s "http://10.42.42.42/flash?url=http://10.42.42.1/files/$selection") ||
echo "Could not reach the device!"
echo "$RESULT"
if [[ "$RESULT" =~ failed || -z "$RESULT" ]]; then
read -p "Do you want to try something else? [y/N] " -n 1 -r
echo
[[ "$REPLY" =~ ^[Yy]$ ]] || break
else
if [[ "$selection" == "tasmota.bin" ]]; then
echo "Look for a tasmota-xxxx SSID to which you can connect and configure"
echo "Be sure to configure your device for proper function!"
elif [[ "$selection" == "espurna.bin" ]]; then
echo "Look for an ESPURNA-XXXXXX SSID to which you can connect and configure"
echo "Default password is \"fibonacci\""
echo "Be sure to upgrade to your device specific firmware for proper function!"
fi
echo
echo "HAVE FUN!"
break
fi
done