-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch.sh
261 lines (229 loc) · 7.97 KB
/
launch.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/bin/sh
echo "$0" "$@"
progdir="$(dirname "$0")"
cd "$progdir" || exit 1
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$progdir/lib"
echo 1 >/tmp/stay_awake
trap "rm -f /tmp/stay_awake" EXIT INT TERM HUP QUIT
BUTTON_LOG="$progdir/log/buttons.log"
SERVICE_NAME="dropbear"
HUMAN_READABLE_NAME="SSH Server"
ONLY_LAUNCH_THEN_EXIT=0
LAUNCHES_SCRIPT="false"
service_on() {
cd "$SDCARD_PATH" || exit 1
if [ -f "$progdir/log/service.log" ]; then
mv "$progdir/log/service.log" "$progdir/log/service.log.old"
fi
trimui_password="$(cat "$progdir/password" 2>/dev/null || true)"
# default password is trimui
# shellcheck disable=SC2016
trimui_password_hash='$1$xyz$4mAm0CgEQkOzH6K/ibOvO1'
if [ -n "$trimui_password" ]; then
# todo: implement me
echo "TODO: Overridie trimui password with specified password"
else
echo "Using default trimui password"
fi
# default is tina
# shellcheck disable=SC2016
root_password_hash='$1$xyz$aO9utGNHk.FAqgCQghNg/1'
if [ -f "$progdir/passwordless-root" ]; then
echo "Using passwordless root"
# shellcheck disable=SC2016
root_password_hash='$1$xyz$kjXWClpYD0.j9bPLUk/Ii.'
else
echo "Using default root password"
fi
if [ -f "$progdir/res/etc/passwd" ]; then
rm -f "$progdir/res/etc/passwd" || return 1
fi
cp "$progdir/res/etc/passwd.template" "$progdir/res/etc/passwd"
sed -i "s:ROOT_PASSWORD:$root_password_hash:g" "$progdir/res/etc/passwd"
sed -i "s:TRIMUI_PASSWORD:$trimui_password_hash:g" "$progdir/res/etc/passwd"
sync --data "$progdir/res/etc/passwd"
# passwd_file="$progdir/res/etc/passwd"
passwd_file="$progdir/res/etc/passwd.default"
chmod 0664 "$passwd_file" "$progdir/res/etc/group"
chown root:root "$passwd_file" "$progdir/res/etc/group"
mount -o bind "$passwd_file" /etc/passwd
mount -o bind "$progdir/res/etc/group" /etc/group
mkdir -p /etc/dropbear
dropbear_bin="$progdir/bin/$SERVICE_NAME"
if [ -f "$progdir/passwordless-root" ]; then
("$dropbear_bin" -R -F -E -B >"$progdir/log/service.log" 2>&1 &) &
else
("$dropbear_bin" -R -F -E >"$progdir/log/service.log" 2>&1 &) &
fi
}
service_off() {
umount /etc/passwd
umount /etc/group
killall "$SERVICE_NAME"
}
show_message() {
message="$1"
seconds="$2"
if [ -z "$seconds" ]; then
seconds="forever"
fi
killall sdl2imgshow
echo "$message"
if [ "$seconds" = "forever" ]; then
"$progdir/bin/sdl2imgshow" \
-i "$progdir/res/background.png" \
-f "$progdir/res/fonts/BPreplayBold.otf" \
-s 27 \
-c "220,220,220" \
-q \
-t "$message" &
else
"$progdir/bin/sdl2imgshow" \
-i "$progdir/res/background.png" \
-f "$progdir/res/fonts/BPreplayBold.otf" \
-s 27 \
-c "220,220,220" \
-q \
-t "$message"
sleep "$seconds"
fi
}
monitor_buttons() {
if [ -f "$BUTTON_LOG" ]; then
mv "$BUTTON_LOG" "$BUTTON_LOG.old"
fi
touch "$BUTTON_LOG"
chmod +x "$progdir/bin/evtest"
for dev in /dev/input/event*; do
[ -e "$dev" ] || continue
"$progdir/bin/evtest" "$dev" 2>&1 | while read -r line; do
if echo "$line" | grep -q "code 17 (ABS_HAT0Y).*value -1"; then
echo "D_PAD_UP detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 17 (ABS_HAT0Y).*value 1"; then
echo "D_PAD_DOWN detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 16 (ABS_HAT0X).*value 1"; then
echo "D_PAD_RIGHT detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 16 (ABS_HAT0X).*value -1"; then
echo "D_PAD_LEFT detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 308 (BTN_WEST).*value 1"; then
echo "BUTTON_X detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 305 (BTN_EAST).*value 1"; then
echo "BUTTON_A detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 304 (BTN_SOUTH).*value 1"; then
echo "BUTTON_B detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 307 (BTN_NORTH).*value 1"; then
echo "BUTTON_Y detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 317 (BTN_THUMBL).*value 1"; then
echo "HOTKEY_1 detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 318 (BTN_THUMBR).*value 1"; then
echo "HOTKEY_2 detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 310 (BTN_TL).*value 1"; then
echo "L1 detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 311 (BTN_TR).*value 1"; then
echo "R1 detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 2 (ABS_Z).*value 255"; then
echo "L2 detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 5 (ABS_RZ).*value 255"; then
echo "R2 detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 316 (BTN_MODE).*value 1"; then
echo "MENU detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 314 (BTN_SELECT).*value 1"; then
echo "SELECT detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 315 (BTN_START).*value 1"; then
echo "START detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 115 (KEY_VOLUMEUP).*value 1"; then
echo "VOLUME_UP detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 114 (KEY_VOLUMEDOWN).*value 1"; then
echo "VOLUME_DOWN detected" >>"$BUTTON_LOG"
elif echo "$line" | grep -q "code 116 (KEY_POWER).*value 1"; then
echo "POWER detected" >>"$BUTTON_LOG"
fi
done &
done
}
wait_for_button() {
button="$1"
while true; do
if grep -q "$button" "$BUTTON_LOG"; then
break
fi
sleep 0.1
done
}
is_service_running() {
if pgrep "$SERVICE_NAME" >/dev/null 2>&1; then
return 0
fi
if [ "$LAUNCHES_SCRIPT" = "true" ]; then
if pgrep -fn "$SERVICE_NAME" >/dev/null 2>&1; then
return 0
fi
fi
return 1
}
wait_for_service() {
max_counter="$1"
counter=0
while ! is_service_running; do
counter=$((counter + 1))
if [ "$counter" -gt "$max_counter" ]; then
return 1
fi
sleep 1
done
}
main_daemonize() {
echo "Toggling $SERVICE_NAME..."
if is_service_running; then
show_message "Disabling the $HUMAN_READABLE_NAME" 2
service_off
else
show_message "Enabling the $HUMAN_READABLE_NAME" 2
service_on
if ! wait_for_service 10; then
show_message "Failed to start $HUMAN_READABLE_NAME" 2
return 1
fi
fi
show_message "Done" 1
}
main_process() {
if is_service_running; then
show_message "Disabling the $HUMAN_READABLE_NAME" 2
service_off
fi
show_message "Starting $HUMAN_READABLE_NAME" 2
service_on
sleep 1
echo "Waiting for $HUMAN_READABLE_NAME to be running"
if ! wait_for_service 10; then
show_message "Failed to start $HUMAN_READABLE_NAME" 2
return 1
fi
show_message "Press B to exit"
monitor_buttons
wait_for_button "BUTTON_B"
show_message "Stopping $HUMAN_READABLE_NAME"
service_off
killall evtest
sync
sleep 1
show_message "Done" 1
}
main() {
if [ "$ONLY_LAUNCH_THEN_EXIT" -eq 1 ]; then
service_on
return $?
fi
if [ -f "$progdir/daemon-mode" ]; then
main_daemonize
else
main_process
fi
killall sdl2imgshow
}
mkdir -p "$progdir/log"
if [ -f "$progdir/log/launch.log" ]; then
mv "$progdir/log/launch.log" "$progdir/log/launch.log.old"
fi
main "$@" >"$progdir/log/launch.log" 2>&1