-
Notifications
You must be signed in to change notification settings - Fork 0
/
ironos_flasher.sh
executable file
·181 lines (152 loc) · 4.74 KB
/
ironos_flasher.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/sh
# path: /home/klassiker/.local/share/repos/ironos-flasher/ironos_flasher.sh
# author: klassiker [mrdotx]
# github: https://github.com/mrdotx/ironos-flasher
# date: 2024-03-30T08:21:21+0100
# auth can be something like sudo -A, doas -- or nothing,
# depending on configuration requirements
auth="${EXEC_AS_USER:-sudo}"
# config
mnt_dir="/tmp/ironos"
max_attempts=2
script=$(basename "$0")
help="$script [-h/--help] -- script for flashing firmware to compatible devices
Usage:
$script [-a/--attempts] <hexfile>
Settings:
[-a/--attempts] = maximum attempts until successfully flash the device
(default: $max_attempts)
<hexfile> = file to be flashed to the device
Examples:
$script TS100_EN.hex
$script -a 1 TS100_EN.hex
$script --attempts 5 TS100_EN.hex
Config:
mnt_dir = $mnt_dir
max_attempts = $max_attempts"
gnome_automount() {
command -v gsettings > /dev/null 2>&1 \
|| return 1
schema='org.gnome.desktop.media-handling'
key='automount'
case "$1" in
disable)
gsettings get "$schema" "$key" \
| grep -q true \
&& automount=1 \
&& gsettings set "$schema" "$key" false
;;
enable)
[ -n "$automount" ] \
&& gsettings set "$schema" "$key" true
esac
}
wait_for_device() {
device_attached() {
dfu_disk=$(lsblk -bro name,model | grep 'DFU.*Disk$') \
|| return 1
device=$(printf "/dev/%s" "$dfu_disk" | cut -d' ' -f1)
instructions=0
}
while ! device_attached; do
[ -z "$instructions" ] \
&& printf "%s\n" \
'#################################################' \
'# #' \
'# Waiting for DFU Disk to appear #' \
'# #' \
'# Connect the soldering iron with a USB cable #' \
'# while holding the button closest to the tip #' \
'# pressed until "DFU: <version>" is displayed #' \
'# #' \
'#################################################' \
&& instructions=0
sleep 1
done
}
mount_device() {
mkdir -p "$mnt_dir"
! $auth mount -t msdos -o umask=0000 "$device" "$mnt_dir" \
&& printf "Failed to mount %s on %s\n" "$device" "$mnt_dir"\
&& exit 1
}
umount_device() {
[ -d "$mnt_dir" ] \
&& ! (mountpoint -q "$mnt_dir" && $auth umount "$mnt_dir") \
&& printf "Failed to unmount %s\n" "$mnt_dir"\
&& exit 1
sleep 1
rm -d "$mnt_dir"
}
check_file() {
if [ ! -f "$1" ] \
|| [ "$(head -c15 "$1")" != ':020000040800F2' ] \
|| [ "$(tail -n1 "$1" | head -c11)" != ':00000001FF' ]; then
printf "%s\n\n %s\n '%s' %s\n %s\n" \
"$help" \
'Error:' \
"$1" \
'does not look like a valid HEX file.' \
'Please provide a regular HEX file to flash...'
exit 1
fi
}
check_integer() {
! [ "$1" -gt 0 ] 2> /dev/null \
&& printf "%s\n\n %s\n '%s' %s\n" \
"$help" \
'Error:' \
"$1" \
'does not look like a positive integer.' \
&& exit 1
}
flash_device() {
max_attempts=$2
gnome_automount disable
while [ "$max_attempts" -gt 0 ]; do
wait_for_device
printf "\nFound DFU Disk on %s\n" "$device"
mount_device
printf "Mounted DFU Disk, flashing...\n"
cat "$1" > "$mnt_dir/firmware.hex"
printf "Remounting DFU Disk\n"
umount_device
wait_for_device
mount_device
result=$(basename \
"$(find "$mnt_dir" -type f -iname 'firmware.*' \
| tail -n1 \
)" \
)
umount_device
if [ "$result" = 'firmware.rdy' ]; then
max_attempts=0
printf "\n Flashing successful!\n"
else
max_attempts=$((max_attempts - 1))
printf "\n %s\n %s %d\n" \
'Flashing not successful, please wait...' \
'Attempts left:' \
"$max_attempts"
fi
done
gnome_automount enable
}
case "$1" in
-h | --help | '')
printf "%s\n" "$help"
;;
-a | --attempts)
check_integer "$2"
max_attempts=$2
shift 2
check_file "$1"
flash_device "$1" "$max_attempts"
printf "\n"
;;
*)
check_file "$1"
flash_device "$1" "$max_attempts"
printf "\n"
;;
esac