-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnpi-config
1325 lines (1210 loc) · 44 KB
/
npi-config
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Part of npi-config http://github.com/friendlyarm/npi-config
#
# See LICENSE file for copyright and license details
INTERACTIVE=True
ASK_TO_REBOOT=0
if [ -f /etc/friendlyelec-release ]; then
# get friendlyelec's boardt model
# example:
# BOARD="NanoPC-T2"
# LINUXFAMILY=nanopi2
. /etc/friendlyelec-release
else
echo "Not found /etc/friendlyelec-release, Unable to determine the board model."
echo "Unsupported OS.Please use FriendlyCore."
exit 1
fi
KERN_VERSION=`uname -r`
BOARD_LC=$(echo $BOARD|tr '[:upper:]' '[:lower:]')
DTB="none"
DTB_R=/sys/firmware/fdt
is_allwinner_mainline_kernel() {
IS_ALLWINNER_M=0
if [ "x${LINUXFAMILY}" = "xAllwinnersun50iw2Family" ]; then
IS_ALLWINNER_M=1
DTB_W=/boot/sun50i-h5-${BOARD_LC}.dtb
elif [ "x${LINUXFAMILY}" = "xsun8i" ]; then
IS_ALLWINNER_M=1
DTB_W=/boot/sun8i-h3-${BOARD_LC}.dtb
if [ "x${BOARD_LC}" = "xnanopi-duo" ]; then
DTB_W=/boot/sun8i-h2-plus-${BOARD_LC}.dtb
fi
else
return 1
fi
}
get_init_sys() {
if command -v systemctl > /dev/null && systemctl | grep -q '\-\.mount'; then
SYSTEMD=1
elif [ -f /etc/init.d/cron ] && [ ! -h /etc/init.d/cron ]; then
SYSTEMD=0
else
echo "Unrecognised init system"
return 1
fi
}
calc_wt_size() {
# NOTE: it's tempting to redirect stderr to /dev/null, so supress error
# output from tput. However in this case, tput detects neither stdout or
# stderr is a tty and so only gives default 80, 24 values
WT_HEIGHT=17
WT_WIDTH=$(tput cols)
if [ -z "$WT_WIDTH" ] || [ "$WT_WIDTH" -lt 60 ]; then
WT_WIDTH=80
fi
if [ "$WT_WIDTH" -gt 178 ]; then
WT_WIDTH=120
fi
WT_MENU_HEIGHT=$(($WT_HEIGHT-7))
}
#
# begin
# turn-wifi-into-apmode shell functions
#
FA_RunCmd() {
[ "$V" = "1" ] && echo "+ ${@}"
eval $@ || return $?
}
emptyfile() {
rm -f $1
touch $1
chmod $2 $1
chown root:root $1
}
init_wifi_ap() {
echo "options bcmdhd op_mode=2" > /etc/modprobe.d/bcmdhd.conf
FA_RunCmd rmmod bcmdhd
sleep 1
cp -f /usr/share/network-conf/ap/interfaces /etc/network/interfaces
cp -f /usr/share/network-conf/ap/hostapd_new.conf /etc/hostapd/hostapd.conf
FA_RunCmd modprobe bcmdhd op_mode=2
sleep 2
return 0
}
init_wifi_net() {
echo "options bcmdhd op_mode=5" > /etc/modprobe.d/bcmdhd.conf
FA_RunCmd rmmod bcmdhd
sleep 1
cp -f /usr/share/network-conf/sta/interfaces /etc/network/interfaces
rm -f /etc/hostapd/hostapd.conf
FA_RunCmd modprobe bcmdhd
sleep 3
return 0
}
APSSID=""
APPASSWORD=""
APPASSWORD2=""
pre_apmode() {
APSSID=$(whiptail --inputbox "Enter Hotspot name (SSID)" 20 60 "friendlyelec-wifiap" 3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then
return 1
fi
if [ -z "$APSSID" ]; then
whiptail --msgbox "Hotspot name cannot be blank." 20 60 1
return 1
fi
if [ ${#APSSID} -le 3 ]; then
whiptail --msgbox "Hotspot name is too short." 20 60 1
return 1
fi
APPASSWORD=$(whiptail --inputbox "Enter password (9 characters long)" 20 60 "123456789" 3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then
return 1
fi
if [ -z "${APPASSWORD}" ]; then
whiptail --msgbox "Password cannot be blank." 20 60 1
return 1
fi
if [ ${#APPASSWORD} -le 8 ]; then
whiptail --msgbox "Password is too short, 9 characters long is better." 20 60 1
return 1
fi
APPASSWORD2=$(whiptail --inputbox "Enter password again" 20 60 "123456789" 3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then
return 1
fi
if [ -z "${APPASSWORD2}" ]; then
whiptail --msgbox "Password2 cannot be blank." 20 60 1
return 1
fi
if [ x"${APPASSWORD}" = x"${APPASSWORD2}" ]; then
cp -f /usr/share/network-conf/ap/hostapd.conf /usr/share/network-conf/ap/hostapd_new.conf
sed -i "s/^ssid=.*/ssid=$APSSID/g" /usr/share/network-conf/ap/hostapd_new.conf
sed -i "s/^wpa_passphrase.*/wpa_passphrase="${APPASSWORD}"/g" /usr/share/network-conf/ap/hostapd_new.conf
else
whiptail --msgbox "The two password didn't match." 20 60 1
return 1
fi
return 0
}
end_apmode() {
whiptail --msgbox "Configuration Saved!\nYou can connect your computer to this board now, \nhotspot detail:\n\nWireless Name: $APSSID\nPassword: $APPASSWORD\n\n" 20 60 1
}
switch_apmode() {
pre_apmode
if [ $? -ne 0 ]; then
return 0
fi
FA_RunCmd ifdown wlan0
sleep 1
FA_RunCmd ifconfig wlan0 down
init_wifi_ap
FA_RunCmd systemctl stop dnsmasq
FA_RunCmd systemctl stop hostapd
FA_RunCmd systemctl stop udhcpd.service
FA_RunCmd systemctl enable udhcpd.service
FA_RunCmd systemctl enable dnsmasq
FA_RunCmd systemctl enable hostapd
sed -i 's/DHCPD_ENABLED="no"/DHCPD_ENABLED="yes"/g' /etc/default/udhcpd
if [ -f /etc/wicd/wireless-settings.conf ]; then
cp /etc/wicd/wireless-settings.conf /etc/wicd/wireless-settings.conf.bak
emptyfile /etc/wicd/wireless-settings.conf 600
fi
if [ -f /etc/network/interfaces.d/wlan0 ]; then
cp /etc/network/interfaces.d/wlan0 /etc/wlan0-bak
rm /etc/network/interfaces.d/wlan0
fi
FA_RunCmd systemctl start udhcpd.service
FA_RunCmd systemctl start dnsmasq
FA_RunCmd systemctl start hostapd
FA_RunCmd /sbin/ifconfig wlan0 192.168.8.1 up
end_apmode
return 0
}
exit_apmode() {
FA_RunCmd ifdown wlan0
sleep 1
FA_RunCmd ifconfig wlan0 down
FA_RunCmd systemctl stop dnsmasq
FA_RunCmd systemctl stop hostapd
FA_RunCmd systemctl stop udhcpd.service
FA_RunCmd systemctl disable dnsmasq
FA_RunCmd systemctl disable hostapd
FA_RunCmd systemctl disable udhcpd.service
sed -i 's/DHCPD_ENABLED="yes"/DHCPD_ENABLED="no"/g' /etc/default/udhcpd
init_wifi_net
sleep 2
return 0
}
#
# end
#
do_about() {
whiptail --msgbox "\
npi-config (v1.1.2)\n
This tool provides a straight-forward way of doing initial
configuration of the NanoPi. Although it can be run
at any time, some of the options may have difficulties if
you have heavily customised your installation.\n\n
Currnet Model: ${BOARD}
Kernel Version: ${KERN_VERSION}\
" 20 70 1
return 0
}
do_change_pass() {
id -u pi
RET=$?
if [ $RET -eq 0 ]; then
whiptail --msgbox "You will now be asked to enter a new password for the pi user" 20 60 1
passwd pi &&
whiptail --msgbox "Password changed successfully" 20 60 1
return 0
fi
id -u fa
RET=$?
if [ $RET -eq 0 ]; then
whiptail --msgbox "You will now be asked to enter a new password for the fa user" 20 60 1
passwd fa &&
whiptail --msgbox "Password changed successfully" 20 60 1
return 0
fi
}
do_configure_keyboard() {
dpkg-reconfigure keyboard-configuration &&
printf "Reloading keymap. This may take a short while\n" &&
invoke-rc.d keyboard-setup start
}
do_change_locale() {
dpkg-reconfigure locales
ASK_TO_REBOOT=1
}
do_change_timezone() {
dpkg-reconfigure tzdata
}
get_wifi_country() {
grep country= /etc/wpa_supplicant/wpa_supplicant.conf | cut -d "=" -f 2
}
do_wifi_country() {
oIFS="$IFS"
if [ "$INTERACTIVE" = True ]; then
IFS="/"
value=$(cat /usr/share/zoneinfo/iso3166.tab | tail -n +26 | tr '\t' '/' | tr '\n' '/')
COUNTRY=$(whiptail --menu "Select the country in which the Pi is to be used" 20 60 10 ${value} 3>&1 1>&2 2>&3)
else
COUNTRY=$1
true
fi
if [ $? -eq 0 ];then
if [ -e /etc/wpa_supplicant/wpa_supplicant.conf ]; then
if grep -q "^country=" /etc/wpa_supplicant/wpa_supplicant.conf ; then
sed -i "s/^country=.*/country=$COUNTRY/g" /etc/wpa_supplicant/wpa_supplicant.conf
else
sed -i "1i country=$COUNTRY" /etc/wpa_supplicant/wpa_supplicant.conf
fi
else
echo "country=$COUNTRY" > /etc/wpa_supplicant/wpa_supplicant.conf
fi
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "Wi-fi country set to $COUNTRY" 20 60 1
fi
ASK_TO_REBOOT=1
fi
IFS=$oIFS
}
do_change_hostname() {
whiptail --msgbox "\
Please note: RFCs mandate that a hostname's labels \
may contain only the ASCII letters 'a' through 'z' (case-insensitive),
the digits '0' through '9', and the hyphen.
Hostname labels cannot begin or end with a hyphen.
No other symbols, punctuation characters, or blank spaces are permitted.\
" 20 70 1
CURRENT_HOSTNAME=`cat /etc/hostname | tr -d " \t\n\r"`
NEW_HOSTNAME=$(whiptail --inputbox "Please enter a hostname" 20 60 "$CURRENT_HOSTNAME" 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
echo $NEW_HOSTNAME > /etc/hostname
sed -i "s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\t$NEW_HOSTNAME/g" /etc/hosts
ASK_TO_REBOOT=1
fi
}
do_ssh() {
if [ -e /var/log/regen_ssh_keys.log ] && ! grep -q "^finished" /var/log/regen_ssh_keys.log; then
whiptail --msgbox "Initial ssh key generation still running. Please wait and try again." 20 60 2
return 1
fi
DEFAULT=--defaultno
if [ $(get_ssh) -eq 0 ]; then
DEFAULT=
fi
if [ "$INTERACTIVE" = True ]; then
whiptail --yesno "Would you like the SSH server to be enabled?" $DEFAULT 20 60 2
RET=$?
else
RET=$1
fi
if [ $RET -eq 0 ]; then
update-rc.d ssh enable &&
invoke-rc.d ssh start &&
STATUS=enabled
elif [ $RET -eq 1 ]; then
update-rc.d ssh disable &&
invoke-rc.d ssh stop &&
STATUS=disabled
else
return $RET
fi
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "The SSH server is $STATUS" 20 60 1
fi
}
do_console() {
sed /etc/systemd/system/getty@tty1.service.d/autologin.conf -i -e "s/\(^ExecStart.*agetty\) --autologin pi/\1/g"
sed /etc/systemd/system/serial-getty@ttyS0.service.d/autologin.conf -i -e "s/\(^ExecStart.*agetty\) --autologin pi/\1/g"
sed /etc/systemd/system/serial-getty@ttyAMA0.service.d/autologin.conf -i -e "s/\(^ExecStart.*agetty\) --autologin pi/\1/g"
sed /etc/systemd/system/serial-getty@ttySAC0.service.d/autologin.conf -i -e "s/\(^ExecStart.*agetty\) --autologin pi/\1/g"
}
do_autologin_console() {
do_console
sed /etc/systemd/system/getty@tty1.service.d/autologin.conf -i -e "s/\(^ExecStart.*agetty\)/\1 --autologin pi/g"
sed /etc/systemd/system/serial-getty@ttyS0.service.d/autologin.conf -i -e "s/\(^ExecStart.*agetty\)/\1 --autologin pi/g"
sed /etc/systemd/system/serial-getty@ttyAMA0.service.d/autologin.conf -i -e "s/\(^ExecStart.*agetty\)/\1 --autologin pi/g"
sed /etc/systemd/system/serial-getty@ttySAC0.service.d/autologin.conf -i -e "s/\(^ExecStart.*agetty\)/\1 --autologin pi/g"
}
do_qte() {
whiptail --yesno "Would you like the Qt/E Demo enabled or disabled?" 20 60 2 \
--yes-button Enable --no-button Disable
RET=$?
if [ $RET -eq 0 ]; then
sed /etc/rc.local -i -e "s/^#\(.*\)\/opt\/QtE-Demo\/run.sh/\1\/opt\/QtE-Demo\/run.sh/g"
rm /etc/systemd/system/getty.target.wants/getty@tty1.service
ln -s /lib/systemd/system/getty@.service /etc/systemd/system/getty.target.wants/getty@tty2.service
whiptail --msgbox "Qt/E Demo enabled" 20 60 1
elif [ $RET -eq 1 ]; then
sed /etc/rc.local -i -e "s/\(.*\)\/opt\/QtE-Demo\/run.sh/#\1\/opt\/QtE-Demo\/run.sh/g"
rm /etc/systemd/system/getty.target.wants/getty@tty2.service
ln -s /lib/systemd/system/getty@.service /etc/systemd/system/getty.target.wants/getty@tty1.service
whiptail --msgbox "Qt/E Demo disabled" 20 60 1
else
return $RET
fi
}
get_boot_wait() {
if test -e /etc/systemd/system/dhcpcd.service.d/wait.conf; then
echo 0
else
echo 1
fi
}
do_boot_wait() {
get_init_sys
if [ $SYSTEMD -eq 0 ]; then
whiptail --msgbox "This option can only be selected when using systemd" 20 60 2
return 1
fi
DEFAULT=--defaultno
if [ $(get_boot_wait) -eq 0 ]; then
DEFAULT=
fi
if [ "$INTERACTIVE" = True ]; then
whiptail --yesno "Would you like boot to wait until a network connection is established?" $DEFAULT 20 60 2
RET=$?
else
RET=$1
fi
if [ $RET -eq 0 ]; then
mkdir -p /etc/systemd/system/dhcpcd.service.d/
cat > /etc/systemd/system/dhcpcd.service.d/wait.conf << EOF
[Service]
ExecStart=
ExecStart=/sbin/dhcpcd -q -w
EOF
STATUS=enabled
elif [ $RET -eq 1 ]; then
rm -f /etc/systemd/system/dhcpcd.service.d/wait.conf
STATUS=disabled
else
return $RET
fi
if [ "$INTERACTIVE" = True ]; then
whiptail --msgbox "Waiting for network on boot is $STATUS" 20 60 1
fi
}
do_set_boot_from_tf_env() {
(cd /boot && {\
sed -i 's/ root=\/dev\/[^ ]* / root=\/dev\/mmcblk0p2 /g' boot.cmd; \
sed -i 's/ data=\/dev\/[^ ]*/ data=\/dev\/mmcblk0p3/g' boot.cmd; \
mkimage -C none -A arm -T script -d boot.cmd boot.scr; \
})
ASK_TO_REBOOT=1;
whiptail --msgbox "${BOARD} should boot from TF card next time you reboot the board." 20 60 1
}
do_set_boot_from_hd_env() {
(cd /boot && {\
sed -i 's/ root=\/dev\/[^ ]* / root=\/dev\/sda1 /g' boot.cmd; \
mkimage -C none -A arm -T script -d boot.cmd boot.scr; \
})
ASK_TO_REBOOT=1
whiptail --msgbox "${BOARD} should boot from hard drive next time you reboot the board." 20 60 1
}
do_set_boot_from_emmc_env() {
(cd /boot && {\
sed -i 's/ root=\/dev\/[^ ]* / root=\/dev\/mmcblk1p2 /g' boot.cmd; \
sed -i 's/ data=\/dev\/[^ ]*/ data=\/dev\/mmcblk1p3/g' boot.cmd; \
mkimage -C none -A arm -T script -d boot.cmd boot.scr; \
})
ASK_TO_REBOOT=1
whiptail --msgbox "${BOARD} should boot from emmc next time you reboot the board." 20 60 1
}
do_boot_from_tf() {
grep 'root=\/dev\/mmcblk0p2' /proc/cmdline 2>&1 > /dev/null
RET=$?
if [ $RET -eq 0 ]; then
whiptail --msgbox "The current setting is this one." 20 60 2
return 0
fi
do_set_boot_from_tf_env
}
do_boot_from_emmc() {
grep 'root=\/dev\/mmcblk1p2' /proc/cmdline 2>&1 > /dev/null
RET=$?
if [ $RET -eq 0 ]; then
whiptail --msgbox "The current setting is this one." 20 60 2
return 0
fi
do_set_boot_from_emmc_env
}
do_boot_from_hd() {
grep 'root=\/dev\/sda1' /proc/cmdline 2>&1 > /dev/null
RET=$?
if [ $RET -eq 0 ]; then
whiptail --msgbox "The current setting is this one." 20 60 2
return 0
fi
is_allwinner_mainline_kernel
if [ $IS_ALLWINNER_M -eq 0 ]; then
whiptail --msgbox "Only allwinner boards with mainline kernel support this option." 20 60 2
return 0
fi
if [ ! -e /dev/sda ]; then
whiptail --msgbox "No hard disk device was found." 20 60 2
return 0
fi
HAS_SYS_ON_HD=0
if [ -e /dev/sda1 ]; then
mount | grep /dev/sda1 2>&1 > /dev/null
RET=$?
if [ $RET -eq 0 ]; then
umount /dev/sda1
fi
mkdir -p /tmp/sda1
mount /dev/sda1 /tmp/sda1
RET=$?
if [ $RET -eq 0 ]; then
if [ -d /tmp/sda1/etc -a -d /tmp/sda1/usr/bin ]; then
HAS_SYS_ON_HD=1
fi
umount /dev/sda1
rm -rf /tmp/sda1
fi
fi
NEED_REBUILD=0
if [ $HAS_SYS_ON_HD -eq 1 ]; then
whiptail --yesno "Found existing system on /dev/sda1, would you like to use it?" 20 60 2
RET=$?
if [ $RET -eq 0 ]; then # yes
# use system on /dev/sda1
NEED_REBUILD=0
else
NEED_REBUILD=1
fi
else
NEED_REBUILD=1
fi
if [ $NEED_REBUILD -eq 1 ]; then
whiptail --yesno "WARNING: Now will create system for hard drive, it will erase all data on this disk, do you want to continue?" 20 60 2
RET=$?
if [ $RET -eq 0 ]; then
# remove all partition
dd if=/dev/zero of=/dev/sda bs=512 count=1 conv=notrunc
# add a partition and format
fdisk /dev/sda << FDISKEOF
n
p
1
w
FDISKEOF
yes | mkfs.ext4 /dev/sda1
mkdir -p /diskmount
mount /dev/sda1 /diskmount
echo "Copying rootfs from TF card to Hard drive, please wait ..."
find /* -maxdepth 0 | \
grep -v "/diskmount" | \
grep -v "/proc" | \
grep -v "/sys" | \
xargs cp -af --target-directory=/diskmount
mkdir /diskmount/proc
chmod 0555 /diskmount/proc
chown root:root /diskmount/proc
mkdir /diskmount/sys
chmod 0555 /diskmount/sys
chown root:root /diskmount/sys
umount /dev/sda1
rm -rf /diskmount
else
# use cancel, do nothing
return 0
fi
fi
do_set_boot_from_hd_env
return 0
}
do_boot_device() {
is_allwinner_mainline_kernel
if [ $IS_ALLWINNER_M -eq 0 ]; then
whiptail --msgbox "Only allwinner boards with mainline kernel support this option." 20 60 2
return 0
fi
if [ ! -e /dev/sda ]; then
if [ ! -e /dev/mmcblk1 ]; then
whiptail --msgbox "No other storage device was found." 20 60 2
return 0
fi
fi
BOOTOPT=$(whiptail --menu "Choose boot device" 20 60 10 \
"D1 Hard drive" "Load the root filesystem from SSD/HD/Usbdisk" \
"D2 TF card" "Load the root filesystem from TF card" \
"D3 emmc" "Load the root filesystem from emmc" \
3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
case "$BOOTOPT" in
D1*) do_boot_from_hd ;;
D2*) do_boot_from_tf ;;
D3*) do_boot_from_emmc ;;
*)
whiptail --msgbox "Programmer error, unrecognised boot option" 20 60 2
return 1
;;
esac
fi
}
do_boot_behaviour() {
# if [ ! -d "/opt/QtE-Demo" ]; then
# whiptail --msgbox "This OS is't UbuntuCore with QtE, I don't know how to configure." 20 60 2
# return 0
# fi
# "Qt/Embedded" "Graphical UI, display system info" \
B1_STATUS="disabled"
B2_STATUS="disabled"
B3_STATUS="disabled"
AUTOLOGIN_CONF=/etc/systemd/system/getty@tty1.service.d/autologin.conf
if grep autologin ${AUTOLOGIN_CONF}>/dev/null; then
B2_STATUS="enabled"
else
B1_STATUS="enabled"
fi
if ! grep QtE-Demo /etc/rc.local | grep "#">/dev/null; then
B3_STATUS="enabled"
fi
BOOTOPT=$(whiptail --menu "Chose boot option" 20 60 10 \
"B1 Console" "Text console, requiring user to login[${B1_STATUS}]" \
"B2 Console Autologin" "Text console, automatically logged in as 'pi' user[${B2_STATUS}]" \
"B3 Qt/Embedded" "Disable or enable Qt/E demo auto startup[${B3_STATUS}]" \
3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
case "$BOOTOPT" in
B1*) do_console ;;
B2*) do_autologin_console ;;
B3*) do_qte ;;
*)
whiptail --msgbox "Programmer error, unrecognised boot option" 20 60 2
return 1
;;
esac
ASK_TO_REBOOT=1
fi
}
# Select system default audio device, only support onboard-codec/HDMI-audio.
do_audio() {
is_allwinner_mainline_kernel
if [ $IS_ALLWINNER_M -eq 0 ]; then
whiptail --msgbox "Only allwinner boards with mainline kernel support this option." 20 60 2
return 0
fi
local cardnum=0
SOUND_DIR=/proc/asound/
for dev in `ls ${SOUND_DIR} | grep -E "card[0-9]"`; do
local index=`echo ${dev} | grep -oE [0-9]`
local card[${index}]=${dev}
local card_desc[${index}]=`aplay -l | grep "card ${index}" | head -n 1 | cut -d ":" -f 2-3 | cut -d " " -f 3- | cut -d "[" -f -2`
if [ -z "${card_desc[${index}]}" ]; then
card_desc[${index}]="soundcard${index}"
fi
cardnum=$((cardnum + 1))
done
local selected_audio="unknown"
if [ -z "$1" ]; then
cur_audio="`grep -o "card [0-9]" /etc/asound.conf | head -n 1 | grep -o [0-9]`"
card_desc[${cur_audio}]=${card_desc[${cur_audio}]}"(X)"
# max soundcard number: 5
selected_audio=$(whiptail --menu "Choose the audio output" 20 60 10 \
"${card[0]}" "${card_desc[0]}" \
"${card[1]}" "${card_desc[1]}" \
"${card[2]}" "${card_desc[2]}" \
"${card[3]}" "${card_desc[3]}" \
"${card[4]}" "${card_desc[4]}" \
3>&1 1>&2 2>&3)
else
selected_audio=$1
fi
if [ ! -z "${selected_audio}" ]; then
if /usr/bin/select_audio.sh ${selected_audio} >/dev/null; then
whiptail --msgbox "Select ${selected_audio} as system default audio device OK." 20 60 1
# no need to reboot
else
whiptail --msgbox "Fail to select ${selected_audio} as system default audio device" 20 60 1
fi
fi
}
# Select system default audio device, only support onboard-codec/HDMI-audio.
do_cpu_freq() {
is_allwinner_mainline_kernel
if [ $IS_ALLWINNER_M -eq 0 ]; then
whiptail --msgbox "Only allwinner H3 with mainline kernel support this option." 20 60 2
return 0
fi
if ! grep sun8i -i /proc/cpuinfo; then
whiptail --msgbox "Only allwinner H3 with mainline kernel support this option." 20 60 2
return 0
fi
freq_desc[1]="1008MHz"
freq_desc[2]="1200MHz"
cur_freq=`fdtget ${DTB_R} /cpus/cpu@0 operating-points | cut -d " " -f 1`
if [ "x${cur_freq}" = "x1008000" ];then
freq_desc[1]="1008MHz(X)"
else
freq_desc[2]="1200MHz(X)"
fi
selected_freq=$(whiptail --menu "Choose the highest frequency" 20 60 10 \
"1" "${freq_desc[1]}" \
"2" "${freq_desc[2]}" \
3>&1 1>&2 2>&3)
if [ -n "${selected_freq}" ];then
ASK_TO_REBOOT=1
fi
case ${selected_freq} in
"1") fdtput ${DTB_W} /cpus/cpu@0 operating-points 1008000 1300000 816000 1100000 624000 1100000 480000 1100000 312000 1100000 240000 1100000 120000 1100000] ;;
"2") fdtput ${DTB_W} /cpus/cpu@0 operating-points 1200000 1300000 816000 1100000 624000 1100000 480000 1100000 312000 1100000 240000 1100000 120000 1100000] ;;
*)
return 0
;;
esac
return 0
}
do_set_up_wifi_hotspot() {
wlan=`ifconfig wlan0`
if [ $? -eq 0 ]; then
WIFI_MODE=$(whiptail --menu "Choose the WiFi mode" 20 60 10 \
"W0" "Run as Access point (AP mode)" \
"W1" "Run as Wireless LAN client (Station mode)" \
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
elif [ $RET -eq 0 ]; then
case "$WIFI_MODE" in
W0) switch_apmode ;;
W1) exit_apmode ;;
*) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running option $FUN" 20 60 1
fi
else
whiptail --msgbox "No WLAN devices found." 20 60 1
fi
}
do_finish() {
if [ $ASK_TO_REBOOT -eq 1 ]; then
whiptail --yesno "Would you like to reboot now?" 20 60 2
if [ $? -eq 0 ]; then # yes
sync
reboot
fi
fi
exit 0
}
#if [ "GET" = "${OPT_MEMORY_SPLIT:-}" ]; then
# set -u # Fail on unset variables
# get_current_memory_split
# echo $CURRENT_MEMSPLIT
# exit 0
#fi
# Everything else needs to be run as root
if [ $(id -u) -ne 0 ]; then
printf "Script must be run as root. Try 'sudo npi-config'\n"
exit 1
fi
do_internationalisation_menu() {
FUN=$(whiptail --title "NanoPi Software Configuration Tool (npi-config)" --menu "Internationalisation Options" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
"I1 Change Locale" "Set up language and regional settings to match your location" \
"I2 Change Timezone" "Set up timezone to match your location" \
"I3 Change Keyboard Layout" "Set the keyboard layout to match your keyboard" \
"I4 Change Wi-fi Country" "Set the legal channels used in your country" \
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
elif [ $RET -eq 0 ]; then
case "$FUN" in
I1\ *) do_change_locale ;;
I2\ *) do_change_timezone ;;
I3\ *) do_configure_keyboard ;;
I4\ *) do_wifi_country ;;
*) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running option $FUN" 20 60 1
fi
}
do_connect_wifi() {
wlan=`ifconfig wlan0`
if [ $? -ne 0 ]; then
whiptail --msgbox "No WLAN devices found." 20 60 1
return 0
fi
if [ -f /sys/module/bcmdhd/parameters/op_mode ]; then
wifimod=`cat /sys/module/bcmdhd/parameters/op_mode`
if [ $wifimod -eq 2 ]; then
whiptail --msgbox "The WiFi is working in AP mode, you need switch to station mode first." 20 60 1
do_set_up_wifi_hotspot
return 0
elif [ $wifimod -eq 5 ]; then
echo "station mode"
else
whiptail --msgbox "Unknow WiFi mode: ${wifimod}" 20 60 1
return 0
fi
else
whiptail --msgbox "Unsupported WiFi module." 20 60 1
return 0
fi
SSID=$(whiptail --inputbox "Enter network name (SSID)" 20 60 "" 3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then
return 0
fi
if [ -z "$SSID" ]; then
whiptail --msgbox "Network name cannot be blank." 20 60 1
return 0
fi
if [ ${#SSID} -le 3 ]; then
whiptail --msgbox "Network name is too short." 20 60 1
return 0
fi
PASSWORD=$(whiptail --inputbox "Enter password (Network name: ${SSID})" 20 60 "" 3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then
return 0
fi
if [ -z "${PASSWORD}" ]; then
whiptail --msgbox "Password cannot be blank." 20 60 1
return 0
else
if [ ${#PASSWORD} -le 4 ]; then
whiptail --msgbox "Password is too short." 20 60 1
return 0
fi
PASSWORD2=$(whiptail --inputbox "Enter password again (Network name: ${SSID})" 20 60 "" 3>&1 1>&2 2>&3)
if [ $? -ne 0 ]; then
return 0
fi
if [ -z "${PASSWORD2}" ]; then
whiptail --msgbox "Password cannot be blank." 20 60 1
return 0
fi
if [ x"${PASSWORD}" = x"${PASSWORD2}" ]; then
cat >/etc/wpa_supplicant/wpa_supplicant.conf <<EOL
update_config=1
network={
ssid="${SSID}"
psk="${PASSWORD}"
}
EOL
whiptail --msgbox "Configuration saved, you may need to reboot device to apply these changes." 20 60 1
ASK_TO_REBOOT=1
else
whiptail --msgbox "The two password didn't match. p1=${PASSWORD}, p2=${PASSWORD2}" 20 60 1
return 0
fi
fi
}
do_wifi_menu() {
FUN=$(whiptail --title "NanoPi Software Configuration Tool (npi-config)" --menu "Wireless settings" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button Back --ok-button Select \
"A1 Connect to a WiFi Network" "" \
"A2 Configure Wi-Fi hotspot" "" \
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -eq 1 ]; then
return 0
elif [ $RET -eq 0 ]; then
case "$FUN" in
A1\ *) do_connect_wifi ;;
A2\ *) do_set_up_wifi_hotspot ;;
*) whiptail --msgbox "Programmer error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running option $FUN" 20 60 1
fi
}
do_welcome_message() {
whiptail --yesno "Would you like the welcome message enabled or disabled?" 20 60 2 \
--yes-button Enable --no-button Disable
RET=$?
if [ $RET -eq 0 ]; then
sed /etc/pam.d/sshd -i -e "s/^#\s*\(session.*pam_motd.so\)/\1/g"
sed /etc/pam.d/login -i -e "s/^#\s*\(session.*pam_motd.so\)/\1/g"
whiptail --msgbox "Welcome message enabled" 20 60 1
elif [ $RET -eq 1 ]; then
sed /etc/pam.d/sshd -i -e "s/^\s*\(session.*pam_motd.so\)/# \1/g"
sed /etc/pam.d/login -i -e "s/^\s*\(session.*pam_motd.so\)/# \1/g"
whiptail --msgbox "Welcome message disabled" 20 60 1
else
return $RET
fi
}
do_get_dtb_status() {
local NODE=$1
RET=`fdtget ${DTB_R} ${NODE} status`
if [ "x${RET}" = "xokay" ]; then
echo "enabled"
else
echo "disabled"
fi
}
do_select_spi0dev() {
local spidev0="spidev0.0"
local spidev1="spi-2.8'tft"
local spidev2="spi-flash"
local spidev0_desc="SPI0 char device"
local spidev1_desc="Matrix-2'8_SPI_Key_TFT"
local spidev2_desc="SPI0 flash"
local selected_dev="spidev0.0"
local spidev0_status=`do_get_dtb_status spidev0 status`
local spidev1_status=`do_get_dtb_status pitft status`
local spidev2_status=`do_get_dtb_status spiflash status`
if [ "x${spidev0_status}" = "xenabled" ];then
spidev0_desc=${spidev0_desc}"(X)"
elif [ "x${spidev1_status}" = "xenabled" ];then
spidev1_desc=${spidev1_desc}"(X)"
elif [ "x${spidev2_status}" = "xenabled" ];then
spidev2_desc=${spidev2_desc}"(X)"
fi
if [ "x${1}" = "x${spidev0}" -o "x${1}" = "x${spidev1}" -o "x${1}" = "x${spidev2}" ]; then
selected_dev=${1}
else
selected_dev=$(whiptail --menu "Choose the spi0 device" 20 60 10 \
"${spidev0}" "${spidev0_desc}" \
"${spidev1}" "${spidev1_desc}" \
"${spidev2}" "${spidev2_desc}" \
3>&1 1>&2 2>&3)
fi
if [ $? -eq 0 ]; then
case ${selected_dev} in
${spidev0})
fdtput --type s ${DTB_W} spidev0 status "okay" || whiptail --msgbox "Fail to enable ${spidev0}" 20 60 1
fdtput --type s ${DTB_W} pitft status "disabled" || whiptail --msgbox "Fail to disable ${spidev1}" 20 60 1
fdtput --type s ${DTB_W} pitft_ts status "disabled" || whiptail --msgbox "Fail to disable ${spidev1}" 20 60 1
fdtput --type s ${DTB_W} spiflash status "disabled" || whiptail --msgbox "Fail to disable ${spidev2}" 20 60 1
do_display fb0
;;
${spidev1})
local warning="WARNING: Enable ${spidev1} will disable serial2"
whiptail --yesno "Enable/Disable ${spidev1} module?\n${warning}" 20 60 2 \
--yes-button Enable --no-button Disable
RET=$?
if [ $RET -eq 0 ]; then
fdtput --type s ${DTB_W} spidev0 status "disabled" || whiptail --msgbox "Fail to disable ${spidev0}" 20 60 1
fdtput --type s ${DTB_W} pitft status "okay" || whiptail --msgbox "Fail to enable ${spidev1}" 20 60 1
fdtput --type s ${DTB_W} pitft_ts status "okay" || whiptail --msgbox "Fail to enable ${spidev1}" 20 60 1
fdtput --type s ${DTB_W} spiflash status "disabled" || whiptail --msgbox "Fail to disable ${spidev2}" 20 60 1
fdtput --type s ${DTB_W} serial2 status "disabled" || whiptail --msgbox "Fail to disable ${serial2}" 20 60 1
do_display fb_st7789s
fi
;;
${spidev2})
fdtput --type s ${DTB_W} spidev0 status "disabled" || whiptail --msgbox "Fail to disable ${spidev0}" 20 60 1
fdtput --type s ${DTB_W} pitft status "disabled" || whiptail --msgbox "Fail to disable ${spidev1}" 20 60 1
fdtput --type s ${DTB_W} pitft_ts status "disabled" || whiptail --msgbox "Fail to disable ${spidev1}" 20 60 1
fdtput --type s ${DTB_W} spiflash status "okay" || whiptail --msgbox "Fail to enable ${spidev2}" 20 60 1
do_display fb0
;;
esac
fi
}
do_select_i2s0dev() {
local selected_dev="pcm5102a"
local i2sdev0="pcm5102a"
local i2sdev0_desc="NanoHat PCM5102A"
local i2sdev0_status=`do_get_dtb_status pcm5102a status`
if [ "x${i2sdev0_status}" = "xenabled" ];then
i2sdev0_desc=${spidev0_desc}"(X)"
fi
selected_dev=$(whiptail --menu "Choose the i2s0 device" 20 60 10 \
"${i2sdev0}" "${i2sdev0_desc}" \
3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
case ${selected_dev} in