-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathreTerminal_overlays.sh
executable file
·299 lines (250 loc) · 8.42 KB
/
reTerminal_overlays.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/bin/bash
# module version
ver="0.1"
# we create a dir with this version to ensure that 'dkms remove' won't delete
# the sources during kernel updates
marker="0.0.0"
FORCE_KERNEL="1.20210303-1"
uname_r=$(uname -r)
arch_r=$(dpkg --print-architecture)
# Common path
SRC_PATH=/usr/src
MOD_PATH=$(pwd)/modules
RES_PATH=$(pwd)/extras/reTerminal/resources
# RPI
CFG_PATH=/boot/config.txt
CLI_PATH=/boot/cmdline.txt
OVERLAY_DIR=/boot/overlays
BOOT_MOD=/etc/modules
# Ubuntu
[ -f /boot/firmware/config.txt ] && CFG_PATH=/boot/firmware/config.txt
[ -f /boot/firmware/cmdline.txt ] && CLI_PATH=/boot/firmware/cmdline.txt
[ -d /boot/firmware/overlays ] && OVERLAY_DIR=/boot/firmware/overlays
_VER_RUN=""
function get_kernel_version() {
local ZIMAGE IMG_OFFSET
[ -z "$_VER_RUN" ] && {
ZIMAGE=/boot/kernel7l.img
if [ $arch_r == "arm64" ]; then
ZIMAGE=/boot/kernel8.img
fi
[ -f /boot/firmware/vmlinuz ] && ZIMAGE=/boot/firmware/vmlinuz
IMG_OFFSET=$(LC_ALL=C grep -abo $'\x1f\x8b\x08\x00' $ZIMAGE | head -n 1 | cut -d ':' -f 1)
_VER_RUN=$(dd if=$ZIMAGE obs=64K ibs=4 skip=$((IMG_OFFSET / 4)) 2>/dev/null | zcat | grep -a -m1 "Linux version" | strings | awk '{ print $3; }')
}
echo "$_VER_RUN"
return 0
}
# Check headers
function check_kernel_headers() {
VER_RUN=$(get_kernel_version)
if [[ -e "/lib/modules/${VER_RUN}/build" ]]; then
echo KBUILD: "/lib/modules/${VER_RUN}/build"
return 0
fi
echo " !!! Your kernel version is $VER_RUN"
echo " Couldn't find *** corresponding *** kernel headers with apt-get."
echo " This may happen if you ran 'rpi-update'."
echo " Choose *** y *** to install kernel-headers to version $(uname -r) and continue."
echo " Choose *** N *** to exit without this driver support, by default."
read -p "Would you like to proceed? (y/N)" -n 1 -r -s
echo
if ! [[ $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
apt-get -y install raspberrypi-kernel-headers
}
function download_install_debpkg() {
local prefix name r pkg status _name
prefix=$1
name=$2
pkg=${name%%_*}
status=$(dpkg -l $pkg | tail -1)
_name=$(echo "$status" | awk '{ printf "%s_%s_%s", $2, $3, $4; }')
status=$(echo "$status" | awk '{ printf "%s", $1; }')
if [ "X$status" == "Xii" -a "X${name%.deb}" == "X$_name" ]; then
echo "debian package $name already installed."
return 0
fi
for ((i = 0; i < 3; i++)); do
wget $prefix$name -O /tmp/$name && break
done
dpkg -i /tmp/$name
r=$?
rm -f /tmp/$name
return $r
}
function install_kernel() {
local _url _prefix
# Instead of retrieving the lastest kernel & headers
[ "X$FORCE_KERNEL" == "X" ] && {
# Raspbian kernel packages
apt-get -y --force-yes install raspberrypi-kernel-headers raspberrypi-kernel || {
# Ubuntu kernel packages
apt-get -y install linux-raspi linux-headers-raspi linux-image-raspi
}
} || {
# We would like to a fixed version
KERN_NAME=raspberrypi-kernel_${FORCE_KERNEL}_${arch_r}.deb
HDR_NAME=raspberrypi-kernel-headers_${FORCE_KERNEL}_${arch_r}.deb
_url=$(apt-get download --print-uris raspberrypi-kernel | sed -nre "s/'([^']+)'.*$/\1/g;p")
_prefix=$(echo $_url | sed -nre 's/^(.*)raspberrypi-kernel_.*$/\1/g;p')
download_install_debpkg "$_prefix" "$KERN_NAME" && {
download_install_debpkg "$_prefix" "$HDR_NAME"
} || {
echo "Error: Install kernel or header failed"
exit 2
}
}
}
# Install module
function install_modules {
if [ $# -eq 0 ]; then
echo "No module to install!"
exit 1
fi
# locate currently installed kernels (may be different to running kernel if
# it's just been updated)
kernel=$(get_kernel_version)
for mod; do
target=$SRC_PATH/$mod-$ver
mkdir -p $target
cp -af $MOD_PATH/$mod/* $target/
dkms build -k ${kernel} -m $mod -v $ver && {
dkms install --force -k ${kernel} -m $mod -v $ver
} || {
echo "Can't compile with this kernel, aborting"
echo "Please try to compile with the option --compat-kernel"
exit 1
}
mkdir -p /var/lib/dkms/$mod/$ver/$marker
done
}
# Overlay
function install_overlay {
if [ $# -eq 0 ]; then
echo "No dtbo to install!"
exit 1
fi
# cmdline.txt
CMDLINE=$(cat $CLI_PATH)
CMDLINE=$(echo $CMDLINE | sed 's/ *\bconsole=tty0\b//g')
grep -q "\blogo.nologo\b" $CLI_PATH ||
CMDLINE="$CMDLINE logo.nologo"
grep -q "\bvt.global_cursor_default=0\b" $CLI_PATH ||
CMDLINE="$CMDLINE vt.global_cursor_default=0"
grep -q "\bconsole=tty3\b" $CLI_PATH ||
CMDLINE="$CMDLINE console=tty3"
grep -q "\bloglevel=0\b" $CLI_PATH ||
CMDLINE="$CMDLINE loglevel=0"
echo $CMDLINE >$CLI_PATH
# config.txt
sed -i "s/.*dtparam=i2c_arm=.*$/dtparam=i2c_arm=on/g" ${CFG_PATH}
grep -q "^enable_uart=1$" $CFG_PATH ||
echo "enable_uart=1" >>$CFG_PATH
grep -q "^dtoverlay=dwc2,dr_mode=host$" $CFG_PATH ||
echo "dtoverlay=dwc2,dr_mode=host" >>$CFG_PATH
grep -q "^dtparam=ant2$" $CFG_PATH ||
echo "dtparam=ant2" >>$CFG_PATH
grep -q "^disable_splash=1$" $CFG_PATH ||
echo "disable_splash=1" >>$CFG_PATH
grep -q "^ignore_lcd=1$" $CFG_PATH ||
echo "ignore_lcd=1" >>$CFG_PATH
grep -q "^dtoverlay=vc4-kms-v3d-pi4$" $CFG_PATH ||
echo "dtoverlay=vc4-kms-v3d-pi4" >>$CFG_PATH
grep -q "^dtoverlay=i2c3,pins_4_5$" $CFG_PATH ||
echo "dtoverlay=i2c3,pins_4_5" >>$CFG_PATH
grep -q "^gpio=13=pu$" $CFG_PATH ||
echo "gpio=13=pu" >>$CFG_PATH
for i; do
grep -q "^dtoverlay=$i$" $CFG_PATH ||
echo "dtoverlay=$i" >>$CFG_PATH
done
}
function uninstall_overlay {
if [ $# -eq 0 ]; then
echo "No dtbo to remove!"
exit 1
fi
# cmdline.txt
CMDLINE=$(cat $CLI_PATH)
CMDLINE=$(echo $CMDLINE | sed 's/ *\blogo.nologo\b//g')
CMDLINE=$(echo $CMDLINE | sed 's/ *\bvt.global_cursor_default=0\b//g')
CMDLINE=$(echo $CMDLINE | sed 's/ *\bconsole=tty3\b//g')
CMDLINE=$(echo $CMDLINE | sed 's/ *\bloglevel=0\b//g')
echo $CMDLINE >$CLI_PATH
# config.txt
sed -i "/^disable_splash=1$/d" ${CFG_PATH}
sed -i "/^ignore_lcd=1$/d" ${CFG_PATH}
sed -i "/^dtoverlay=vc4-kms-v3d-pi4$/d" ${CFG_PATH}
sed -i "/^dtoverlay=i2c3,pins_4_5$/d" ${CFG_PATH}
sed -i "/^gpio=13=pu$/d" ${CFG_PATH}
for i; do
#rm -fv $OVERLAY_DIR/$i.dtbo || exit 1
sed -i "/^dtoverlay="$i"$/d" ${CFG_PATH}
done
}
function setup_overlay {
sed -i "/^dtoverlay=$1$/s//dtoverlay=$1,$2/" ${CFG_PATH}
}
#NOTICE: this function must be used
# before the uninstall_overlay
function unsetup_overlay {
sed -i "/^dtoverlay=$1,$2$/s//dtoverlay=$1/" ${CFG_PATH}
}
function usage() {
cat <<-__EOF__
usage: sudo ./scripts/reTerminal.sh [ --autoremove | --install ] [ -h | --help ]
default action is update kernel & headers to latest version.
--compat-kernel uses an older kernel but ensures that the driver can work.
--keep-kernel don't change/update the system kernel, maybe install
coressponding kernel headers.
--autoremove used for automatic cleaning
--help show this help message
__EOF__
exit 1
}
function install {
install_overlay reTerminal-overlay reTerminal-bridge-overlay
setup_overlay reTerminal-overlay tp_rotate=1
#audio
if [ -f "/var/lib/alsa/asound.state" ]; then
cp /var/lib/alsa/asound.state /var/lib/alsa/asound.state.bak
fi
if [ -f "/etc/asound.conf" ]; then
cp /etc/asound.conf /etc/asound.conf.bak
fi
cp /usr/local/share/reterminal/wm8960_asound.state /var/lib/alsa/asound.state
cp /usr/local/share/reterminal/asound_2mic.conf /etc/asound.conf
alsactl -L restore
echo "------------------------------------------------------"
echo "Please reboot your device to apply all settings"
echo "Enjoy!"
echo "------------------------------------------------------"
}
function uninstall {
unsetup_overlay reTerminal-overlay tp_rotate=1
uninstall_overlay reTerminal-overlay reTerminal-bridge-overlay
}
function install_mod_boot {
grep -q "^als_ltr30x$" $BOOT_MOD ||
echo als_ltr30x >>$BOOT_MOD
grep -q "^bq24179_charger$" $BOOT_MOD ||
echo bq24179_charger >>$BOOT_MOD
grep -q "^lis331dlh-i2c$" $BOOT_MOD ||
echo lis331dlh-i2c >>$BOOT_MOD
grep -q "^mipi_dsi$" $BOOT_MOD ||
echo mipi_dsi >>$BOOT_MOD
}
function uninstall_mod_boot {
sed -i "/^als_ltr30x$/d" $BOOT_MOD
sed -i "/^bq24179_charger$/d" $BOOT_MOD
sed -i "/^lis331dlh-i2c$/d" $BOOT_MOD
sed -i "/^mipi_dsi$/d" $BOOT_MOD
}
# Check root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root (use sudo)" 1>&2
exit 1
fi
install