-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_env.sh
executable file
·521 lines (429 loc) · 12.6 KB
/
setup_env.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!/bin/bash
set -e
#----------------------------------#
# Global definitions section #
#----------------------------------#
SCRIPT_DIR="$(readlink -f $(dirname $0))"
source $SCRIPT_DIR/lib/utils.sh
source $SCRIPT_DIR/lib/update_release.sh
SUPPORTED_BOARDS="pluto m2k"
PATH="$SCRIPT_DIR/work/openocd-0.10.0/installed/bin:$PATH"
UDEV_RULES_FILE="50-ftdi-test.rules"
INIT_PINS_SCRIPT="$SCRIPT_DIR"/init.sh
UDEV_SECTION='
SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"0456\", ATTRS{idProduct}==\"f001\", MODE=\"660\", ATTRS{serial}==\"Test-Slot-A\", SYMLINK+=\"ttyTest-A%n\"
SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"0456\", ATTRS{idProduct}==\"f001\", MODE=\"660\", ATTRS{serial}==\"Test-Slot-B\", SYMLINK+=\"ttyTest-B%n\"
SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"0456\", ATTRS{idProduct}==\"f001\", MODE=\"660\", ATTRS{serial}==\"Test-Slot-C\", SYMLINK+=\"ttyTest-C%n\"
SUBSYSTEM==\"tty\", ATTRS{idVendor}==\"0456\", ATTRS{idProduct}==\"f001\", MODE=\"660\", ATTRS{serial}==\"Test-Slot-D\", SYMLINK+=\"ttyTest-D%n\"
SUBSYSTEM==\"usb\", ATTRS{idVendor}==\"0456\", ATTRS{idProduct}==\"f001\", MODE=\"660\", GROUP=\"plugdev\"
'
UDEV_SECTION_PINS="SUBSYSTEM==\\\"usb\\\", ATTRS{idVendor}==\\\"0456\\\", ATTRS{idProduct}==\\\"f001\\\", MODE=\\\"660\\\", RUN+=\\\"$INIT_PINS_SCRIPT\\\""
#----------------------------------#
# Functions section #
#----------------------------------#
sudo_required() {
type sudo &> /dev/null || {
echo_red "'sudo' utility required"
exit 1
}
}
setup_apt_install_prereqs() {
type apt-get &> /dev/null || {
echo "No 'apt-get' found; cannot install dependencies"
return 0
}
sudo_required
sudo -s <<-EOF
apt-get -y update
apt-get -y install libftdi-dev bc sshpass openocd \
cmake build-essential git libxml2-dev bison flex \
libfftw3-dev expect usbutils dfu-util screen \
wget unzip curl \
libusb-dev libusb-1.0-0-dev htpdate xfce4-terminal \
openssh-server gpg
/etc/init.d/htpdate restart
EOF
}
build_openocd_0_10_0() {
local url=https://sourceforge.net/projects/openocd/files/openocd/0.10.0/openocd-0.10.0.tar.gz/download
mkdir -p work
wget "$url" -O work/openocd-0.10.0.tar.gz
sudo apt-get -y install libjim-dev
pushd work/
tar -xvf openocd-0.10.0.tar.gz
pushd openocd-0.10.0
./configure --enable-ftdi --disable-internal-jimtcl --prefix="$(pwd)/installed"
make -j3
make install
popd
popd
}
setup_openocd() {
openocd_is_minimum_required || {
echo_red "OpenOCD needs to be at least version 0.10.0"
# if we have apt-get, we can try to build it and install deps too
type apt-get &> /dev/null || exit 1
build_openocd_0_10_0
}
}
check_udev_on_system() {
[ -d "/etc/udev/rules.d" ] || {
echo "Your system does not have a '/etc/udev/rules.d'"
exit 1
}
type udevadm &> /dev/null || {
echo "No 'udevadm' found on system"
exit 1
}
return 0
}
# sync udev rules file
setup_sync_udev_rules_file() {
sudo_required
check_udev_on_system || exit 1
sudo -s <<-EOF
echo -n "$UDEV_SECTION" > "/etc/udev/rules.d/$UDEV_RULES_FILE"
echo "$UDEV_SECTION_PINS" >> "/etc/udev/rules.d/$UDEV_RULES_FILE"
udevadm control --reload-rules
udevadm trigger
EOF
return $?
}
__common_build_tool() {
local c_files
mkdir -p work
for c_file in $tool_c ; do
cp -f "src/$c_file" "work/$c_file"
c_files="$c_files work/$c_file"
done
gcc $c_files -o "work/$tool" $cflags $ldflags
}
setup_ft4232h_tool() {
local tool="ft4232h_pin_ctrl"
local tool_c="${tool}.c ad7616.c platform_drivers.c"
local cflags="-I./src -Werror -Wall"
local ldflags="-lftdi"
tool_c="${tool_c} ft4232h_bitbang.c ft4232h_spi_adc.c ft4232h_spi_eeprom.c"
tool_c="${tool_c} ft4232h_spi_gpio_exp.c"
__common_build_tool
}
__download_github_common() {
local gh_prj="$1"
mkdir -p work
local ver=$(get_latest_release analogdevicesinc/$gh_prj)
[ -d work/$gh_prj ] || {
if [ -z "$ver" ] ; then
echo_red "Could not get $gh_prj release tag; cloning repo"
git clone https://github.com/analogdevicesinc/$gh_prj work/$gh_prj
else
echo_green "Using latest released version '$ver' of '$gh_prj'"
download_and_unzip_to "https://github.com/analogdevicesinc/$gh_prj/archive/${ver}.zip" "work" || {
echo_red "Could not download $gh_prj..."
exit 1
}
mv -f work/${gh_prj}* work/$gh_prj
fi
}
}
setup_libiio() {
[ ! -d "work/libiio" ] || return 0
__download_github_common libiio
pushd work/libiio
mkdir -p build
pushd build
cmake ..
make -j3
popd
popd
}
setup_plutosdr_scripts() {
local cflags="-I../libiio -Wall -Wextra"
local ldflags="-L../libiio/build -lfftw3 -lpthread -liio -lm"
if [ "${BOARD}" != "pluto" ] ; then
echo_blue "Not installing plutosdr_scripts ; board needs to be 'pluto'"
return 0
fi
setup_libiio
[ -d work/plutosdr_scripts ] || \
git clone \
https://github.com/analogdevicesinc/plutosdr_scripts \
work/plutosdr_scripts
pushd work/plutosdr_scripts
gcc -g -o cal_ad9361 cal_ad9361.c $cflags $ldflags
popd
}
setup_scopy() {
if [ "${BOARD}" != "m2k" ] ; then
echo_blue "Not installing scopy ; board needs to be 'm2k'"
return 0
fi
[ ! -d work/scopy ] || return 0
__download_github_common scopy
pushd work/scopy
./CI/travis/before_install_linux.sh
./CI/travis/make.sh
popd
}
setup_write_autostart_config() {
local autostart_path="$HOME/.config/autostart"
local configs_disable="blueman light-locker polkit-gnome-authentication-agent-1"
configs_disable="$configs_disable print-applet pulseaudio snap-userd-autostart"
configs_disable="$configs_disable spice-vdagent update-notifier user-dirs-update-gtk xdg-user-dirs"
mkdir -p $autostart_path
for cfg in $configs_disable ; do
cat > $autostart_path/$cfg.desktop <<-EOF
[Desktop Entry]
Hidden=true
EOF
done
local font_size="16"
if [ "$BOARD" == "pluto" ] ; then
font_size=14
fi
# FIXME: see about generalizing this to other desktops [Gnome, MATE, LXDE, etc]
cat > $autostart_path/test-jig-tool.desktop <<-EOF
[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=test-jig-tool
Comment=test-jig-tool
Exec=sudo xfce4-terminal --font="DejaVu Sans Mono $font_size" --fullscreen --hide-borders --hide-scrollbar --hide-menubar -x $SCRIPT_DIR/production_${BOARD}.sh
OnlyShowIn=XFCE;LXDE
StartupNotify=false
Terminal=false
Hidden=false
EOF
if type ufw &> /dev/null ; then
sudo ufw enable
sudo ufw allow ssh
fi
mkdir -p "$HOME/.ssh"
cat "$SCRIPT_DIR/config/jig_id.pub" >> "$HOME/.ssh/authorized_keys"
sudo chown "$USER.$USER" "$HOME/.ssh/authorized_keys"
chmod 0600 "$HOME/.ssh/authorized_keys"
sudo chown "$USER.$USER" "$SCRIPT_DIR/config/jig_id"
chmod 0600 "$SCRIPT_DIR/config/jig_id"
cat > $autostart_path/call-home.desktop <<-EOF
[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=call-home
Comment=call-home
Exec=/bin/bash $SCRIPT_DIR/call_home
StartupNotify=false
Terminal=false
Hidden=false
EOF
cat > $autostart_path/auto-save-logs.desktop <<-EOF
[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=auto-save-logs
Comment=auto-save-logs
Exec=sudo /bin/bash $SCRIPT_DIR/autosave_logs.sh
StartupNotify=false
Terminal=false
Hidden=false
EOF
cat > $autostart_path/auto-upload-logs.desktop <<-EOF
[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=auto-save-logs
Comment=auto-save-logs
Exec=/bin/bash $SCRIPT_DIR/autoupload_logs.sh
StartupNotify=false
Terminal=false
Hidden=false
EOF
}
board_is_supported() {
local board="$1"
[ -n "$board" ] || return 1
for b in $SUPPORTED_BOARDS ; do
[ "$b" != "$board" ] || return 0
done
return 1
}
xfconf_has_cap() {
type xfconf-query &> /dev/null || return 1
if xfconf-query -l | grep -q xfce4-power-manager ; then
return 0
fi
return 1
}
setup_xfce4_power_manager_settings() {
local pm_sett="/xfce4-power-manager/blank-on-ac=0
/xfce4-power-manager/blank-on-battery=0
/xfce4-power-manager/brightness-switch=0
/xfce4-power-manager/brightness-switch-restore-on-exit=1
/xfce4-power-manager/dpms-enabled=false
/xfce4-power-manager/dpms-on-ac-off=60
/xfce4-power-manager/dpms-on-ac-sleep=20
/xfce4-power-manager/dpms-on-battery-off=30
/xfce4-power-manager/dpms-on-battery-sleep=15
/xfce4-power-manager/lock-screen-suspend-hibernate=true
/xfce4-power-manager/logind-handle-lid-switch=false
/xfce4-power-manager/power-button-action=4
/xfce4-power-manager/show-panel-label=0"
xfconf_has_cap xfce4-power-manager || return 0
for sett in $pm_sett ; do
local key="$(echo $sett | cut -d'=' -f1)"
local val="$(echo $sett | cut -d'=' -f2)"
xfconf-query -c xfce4-power-manager -p $key -s $val
done
}
setup_disable_sudo_passwd() {
sudo_required
sudo -s <<-EOF
if ! grep -q $USER /etc/sudoers ; then
echo "$USER ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers
fi
EOF
}
setup_thunar_volman() {
local configs="/autobrowse/enabled=false
/autoburn/enabled=false
/autoipod/enabled=false
/autokeyboard/enabled=false
/automount-drives/enabled=true
/automount-media/enabled=true
/automouse/enabled=false
/autoopen/enabled=false
/autophoto/enabled=false
/autoplay-audio-cds/enabled=false
/autoplay-video-cds/enabled=false
/autoprinter/enabled=false
/autorun/enabled=false
/autotablet/enabled=false"
xfconf_has_cap thunar-volman || return 0
for sett in $configs ; do
local key="$(echo $sett | cut -d'=' -f1)"
local val="$(echo $sett | cut -d'=' -f2)"
xfconf-query -c thunar-volman -p $key -s $val
done
}
setup_disable_lxde_automount() {
[ -d "$HOME/.config/pcmanfm" ] || return 0
pushd "$HOME/.config/pcmanfm/"
for cfg_file in $(find . -name pcmanfm.conf) ; do
sed 's/autorun=1/autorun=0/g' -i $cfg_file
done
popd
}
setup_pi_boot_config() {
[ "$BOARD" == "pluto" ] || return 0
[ -f /boot/config.txt ] || return 0
local tmp=$(mktemp)
cat >> $tmp <<-EOF
# --- added by setup_env.sh
hdmi_force_hotplug=1
hdmi_group=2
hdmi_mode=87
hdmi_cvt=800 480 60 6 0 0 0
hdmi_drive=1
max_usb_current=1
dtoverlay=pi3-disable-wifi
dtoverlay=pi3-disable-bt
# --- end setup_env.sh
EOF
sudo -s <<-EOF
sed -i -e "/^# --- added by setup_env.sh/,/^# --- end setup_env.sh/d" /boot/config.txt
cat $tmp >> /boot/config.txt
rm -f $tmp
EOF
}
setup_disable_pi_screen_blanking() {
local pi_serial="$(pi_serial)"
[ -n "$pi_serial" ] || return 0
local tmp=$(mktemp)
cat >> $tmp <<-EOF
# --- added by setup_env.sh
[SeatDefaults]
xserver-command=X -s 0 -dpms
# --- end setup_env.sh
EOF
sudo -s <<-EOF
sed -i -e "/^# --- added by setup_env.sh/,/^# --- end setup_env.sh/d" /etc/lightdm/lightdm.conf
cat $tmp >> /etc/lightdm/lightdm.conf
EOF
}
setup_raspi_config() {
sudo -s <<-EOF
if type raspi-config &> /dev/null ; then
raspi-config nonint do_ssh 0 # enable SSH
fi
EOF
}
setup_misc_profile_cleanup() {
touch $HOME/.hushlogin # tell login to not print system info
sudo -s <<-EOF
# Kind of hacky, but it works ; this will remove the warnings
# about the SSH password & Wi-Fi on console login
[ ! -f /etc/profile.d/sshpwd.sh ] || echo -n > /etc/profile.d/sshpwd.sh
[ ! -f /etc/profile.d/wifi-country.sh ] || echo -n > /etc/profile.d/wifi-country.sh
[ ! -f /etc/xdg/lxsession/LXDE-pi/sshpwd.sh ] || echo -n > /etc/xdg/lxsession/LXDE-pi/sshpwd.sh
EOF
}
setup_release_files() {
./update_${BOARD}_release.sh
}
setup_usbreset_tool() {
local tool="usbreset"
local tool_c="${tool}.c"
local cflags="-I./src -Werror -Wall"
local ldflags=""
__common_build_tool
}
setup_zerotier_vpn() {
curl -s https://install.zerotier.com/ | sudo bash
sudo zerotier-cli join d3ecf5726dcec114
}
setup_bashrc_update() {
sed -i -e "/^# --- added by setup_env.sh/,/^# --- end setup_env.sh/d" "$HOME/.bashrc"
cat >> $HOME/.bashrc <<-EOF
# --- added by setup_env.sh
export SCRIPT_DIR="$SCRIPT_DIR"
source "$SCRIPT_DIR/vars.sh"
# --- end setup_env.sh
EOF
}
#----------------------------------#
# Main section #
#----------------------------------#
BOARD="$1"
TARGET="$2"
if [ `id -u` == "0" ]
then
echo_red "This script should not be run as root" 1>&2
exit 1
fi
board_is_supported "$BOARD" || {
echo_red "Board '$BOARD' is not supported by this script"
echo_red " Supported boards are '$SUPPORTED_BOARDS'"
exit 1
}
pushd $SCRIPT_DIR
STEPS="bashrc_update disable_sudo_passwd misc_profile_cleanup raspi_config xfce4_power_manager_settings"
STEPS="$STEPS thunar_volman disable_lxde_automount apt_install_prereqs openocd ft4232h_tool"
STEPS="$STEPS libiio scopy plutosdr_scripts sync_udev_rules_file write_autostart_config"
STEPS="$STEPS pi_boot_config disable_pi_screen_blanking usbreset_tool release_files"
STEPS="$STEPS zerotier_vpn"
RAN_ONCE=0
for step in $STEPS ; do
if [ "$TARGET" == "$step" ] || [ "$TARGET" == "jig" ] ; then
setup_$step
RAN_ONCE=1
fi
done
if [ "$RAN_ONCE" == "0" ] ; then
echo_red "Invalid build target '$TARGET'; valid targets are 'jig' or:"
for step in $STEPS ; do
echo_red " $step"
done
fi
popd