forked from intelligent-agent/Refactor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.sh
executable file
·1534 lines (1359 loc) · 44.9 KB
/
functions.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
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 -e
#
# This script is a library of common functions used by the other scripts in the current directory
# It is meant to be sourced by the other scripts, NOT executed.
# Source it like this:
# source $(dirname "$0")/functions.sh
version_message="1.20170427: u-boot v2017.05-rc2..."
emmcscript="cmdline=init=/opt/scripts/tools/eMMC/$(basename $0)"
#
#https://rcn-ee.com/repos/bootloader/am335x_evm/
http_spl="MLO-am335x_evm-v2017.05-rc2-r4"
http_uboot="u-boot-am335x_evm-v2017.05-rc2-r4.img"
set -o errtrace
trap _exit_trap EXIT
trap _err_trap ERR
_showed_traceback=f
_exit_trap() {
local _ec="$?"
if [[ $_ec != 0 && "${_showed_traceback}" != t ]]; then
_traceback 1
fi
reset_leds 'default-on' || true
}
_err_trap() {
local _ec="$?"
local _cmd="${BASH_COMMAND:-unknown}"
_traceback 1
_showed_traceback=t
echo "The command ${_cmd} exited with exit code ${_ec}." 1>&2
teardown_environment
}
_traceback() {
# Hide the _traceback() call.
local -i start=$(( ${1:-0} + 1 ))
local -i end=${#BASH_SOURCE[@]}
local -i i=0
local -i j=0
echo "Traceback (last called is first):" 1>&2
for ((i=${start}; i < ${end}; i++)); do
j=$(( $i - 1 ))
local function="${FUNCNAME[$i]}"
local file="${BASH_SOURCE[$i]}"
local line="${BASH_LINENO[$j]}"
echo " ${function}() in ${file}:${line}" 1>&2
done
}
__dry_run__(){
generate_line 80 '!'
echo_broadcast "! WARNING: ACTIVATED DRY RUN MODE"
echo_broadcast "! WARNING: THE DRY RUN MODE IS NOT REALLY SAFE"
echo_broadcast "! WARNING: IT IS GOING TO FAIL WITH IO TO FILES"
echo_broadcast "! WARNING: USE AT YOUR OWN RISK"
generate_line 80 '!'
empty_line
#This is useful when debugging scripts with potentially destructive commands
dd() {
echo "!!! Would run 'dd' with '$@'"
}
export -f dd
reboot() {
echo "!!! Would run 'reboot' with '$@'"
}
export -f reboot
modprobe() {
echo "!!! Would run 'modprobe' with '$@'"
}
export -f modprobe
mkfs.vfat() {
echo "!!! Would run 'mkfs.vfat' with '$@'"
}
export -f mkfs.vfat
mkfs.ext4() {
echo "!!! Would run 'mkfs.ext4' with '$@'"
}
export -f mkfs.ext4
sfdisk() {
echo "!!! Would run 'sfdisk' with '$@'"
}
export -f sfdisk
mkdir() {
echo "!!! Would run 'mkdir' with '$@'"
}
export -f mkdir
rsync() {
echo "!!! Would run 'rsync' with '$@'"
}
export -f rsync
mount() {
echo "!!! Would run 'mount' with '$@'"
}
export -f mount
umount() {
echo "!!! Would run 'umount' with '$@'"
}
export -f umount
cp() {
echo "!!! Would run 'cp' with '$@'"
}
export -f cp
}
mmc_mount_fail() {
generate_line 80 '='
echo_broadcast "====> grep mmc"
dmesg | grep mmc || true
generate_line 40
echo_broadcast "====> ls /sys/class/block/"
ls -l /sys/class/block/ || true
generate_line 40
echo_broadcast "====> ls /dev/mmc*"
ls -l /dev/mmc* || true
generate_line 80 '='
}
try_vfat() {
echo_broadcast "====> Mounting ${boot_drive} Read Only over /boot/uboot (trying vfat)"
mount -t vfat ${boot_drive} /boot/uboot/ -o ro || mmc_mount_fail
}
prepare_environment() {
generate_line 80 '='
echo_broadcast "Prepare environment for flashing"
start_time=$(date +%s)
echo_broadcast "Starting at $(date --date="@$start_time")"
generate_line 40
echo_broadcast "==> Giving system time to stablize..."
countdown 5
echo_broadcast "==> Preparing /tmp"
mount -t tmpfs tmpfs /tmp
echo_broadcast "==> Preparing sysctl"
value_min_free_kbytes=$(sysctl -n vm.min_free_kbytes)
echo_broadcast "==> sysctl: vm.min_free_kbytes=[${value_min_free_kbytes}]"
echo_broadcast "==> sysctl: setting: [sysctl -w vm.min_free_kbytes=16384]"
sysctl -w vm.min_free_kbytes=16384
generate_line 40
echo_broadcast "==> Determining root drive"
find_root_drive
echo_broadcast "====> Root drive identified at [${root_drive}]"
boot_drive=${root_drive%?}1
echo_broadcast "==> Boot Drive [${boot_drive}]"
echo_broadcast "==> Figuring out Source and Destination devices"
if [ "x${boot_drive}" = "x/dev/mmcblk0p1" ] ; then
source="/dev/mmcblk0"
destination="/dev/mmcblk1"
elif [ "x${boot_drive}" = "x/dev/mmcblk1p1" ] ; then
source="/dev/mmcblk1"
destination="/dev/mmcblk0"
else
echo_broadcast "!!! Could not reliably determine Source and Destination"
echo_broadcast "!!! We need to stop here"
teardown_environment
write_failure
exit 2
fi
echo_broadcast "====> Source identified: [${source}]"
echo_broadcast "====> Destination identified: [${destination}]"
echo_broadcast "==> Figuring out machine"
get_device
echo_broadcast "====> Machine is ${machine}"
if [ "x${is_bbb}" = "xenable" ] ; then
echo_broadcast "====> Machine is compatible with BeagleBone Black"
fi
if [ ! "x${boot_drive}" = "x${root_drive}" ] ; then
echo_broadcast "====> The Boot and Root drives are identified to be different."
echo_broadcast "====> Giving system time to stablize..."
countdown 5
echo_broadcast "====> Mounting ${boot_drive} Read Only over /boot/uboot"
mount ${boot_drive} /boot/uboot -o ro || try_vfat
fi
generate_line 80 '='
}
_umikaze_backup_directory=""
backup_umikaze_settings() {
generate_line 80 '='
echo_broadcast "creating/discovering storage partition"
partitioner_output=$(/opt/scripts/tools/eMMC/partition.py || true)
echo_broadcast "partitioner output:"
echo_broadcast "$partitioner_output"
storage_partition=$(echo "$partitioner_output" | grep "storage partition ready" | awk '{print $NF}')
if [ "x${storage_partition}" != "x" ] ; then
echo_broadcast "==> Found storage partition - making a filesystem"
mkfs.ext4 -F ${storage_partition}
echo_broadcast "====> Created filesystem"
echo_broadcast "====> Copying files into it"
input_dir=/tmp/input
output_dir=/tmp/storage
echo_broadcast "available partitions:"
echo_broadcast `ls /dev/mmc*`
mkdir $input_dir
echo "mounting $destination as $input_dir"
mount $destination"p1" $input_dir
mkdir $output_dir
echo "mounting $storage_partition as $output_dir"
mount $storage_partition $output_dir
if [ -f $input_dir/etc/kamikaze-release ]; then
generate_line 80 '='
echo "Detected a kamikaze-release file, backup of configs beginning."
generate_line 80 '='
if [ -d $input_dir/home/octo/.octoprint ]; then
echo "updating octoprint's name with the new Umikaze release name"
# use sed to modify the octoprint config.yaml to register the new Umikaze version
VERSION=`cat /etc/kamikaze-release | awk -F ' ' '{print $1 $2}'`
sed -i "s/name: .*kaze.*/name: $VERSION/" $input_dir/home/octo/.octoprint/config.yaml
echo "done"
HOSTNAME=`cat /etc/hostname`
sed -i "s,kamikaze.local:8080/?action=stream,$HOSTNAME:8080/?action=stream," $input_dir/home/octo/.octoprint/config.yaml
sed -i "s,kamikaze.local:8080/?action=snapshot,localhost:8080/?action=snapshot," $input_dir/home/octo/.octoprint/config.yaml
# clear the logs from the .octoprint folder
if [ -f $input_dir/home/octo/.octoprint/logs/plugin_redeem.log ]; then
rm $input_dir/home/octo/.octoprint/logs/plugin_redeem.log
fi
if [ -f $input_dir/home/octo/.octoprint/logs/octoprint.log ]; then
rm $input_dir/home/octo/.octoprint/logs/octoprint.log*
fi
if [ -f $input_dir/home/octo/.octoprint/logs/serial.log ]; then
rm $input_dir/home/octo/.octoprint/logs/serial.log*
fi
fi
get_rsync_options
rsync -aAxv $rsync_options --files-from=/opt/scripts/tools/eMMC/umikaze-files --ignore-missing-args $input_dir $output_dir
_umikaze_backup_directory="$output_dir"
# the octoprint UID may have changed across releases - fix it up now
chown -R octo:octo $output_dir/etc/redeem/
chown -R octo:octo $output_dir/home/octo/
chown -R octo:octo $output_dir/etc/toggle/
fi
generate_line 80 '='
echo_broadcast "BACKUP FINISHED"
generate_line 80 '='
echo "##################################"
echo "Unmount of eMMC"
umount $input_dir
echo "done"
rmdir $input_dir
echo "done"
fi
}
prepare_environment_reverse() {
generate_line 80 '='
echo_broadcast "Prepare environment for flashing"
start_time=$(date +%s)
echo_broadcast "Starting at $(date --date="@$start_time")"
generate_line 40
value_min_free_kbytes=$(sysctl -n vm.min_free_kbytes)
echo_broadcast "==> sysctl: vm.min_free_kbytes=[${value_min_free_kbytes}]"
echo_broadcast "==> sysctl: setting: [sysctl -w vm.min_free_kbytes=16384]"
sysctl -w vm.min_free_kbytes=16384
generate_line 40
# echo_broadcast "==> Preparing /tmp"
# mount -t tmpfs tmpfs /tmp
echo_broadcast "==> Determining root drive"
find_root_drive
echo_broadcast "====> Root drive identified at ${root_drive}"
echo_broadcast "==> Determining boot drive"
boot_drive="${root_drive%?}1"
# if [ ! "x${boot_drive}" = "x${root_drive}" ] ; then
# echo_broadcast "====> The Boot and Root drives are identified to be different."
# echo_broadcast "====> Mounting ${boot_drive} Read Only over /boot/uboot"
# mount ${boot_drive} /boot/uboot -o ro
# fi
echo_broadcast "==> Figuring out Source and Destination devices"
if [ "x${boot_drive}" = "x/dev/mmcblk0p1" ] ; then
source="/dev/mmcblk0"
destination="/dev/mmcblk1"
elif [ "x${boot_drive}" = "x/dev/mmcblk1p1" ] ; then
source="/dev/mmcblk1"
destination="/dev/mmcblk0"
else
echo_broadcast "!!! Could not reliably determine Source and Destination"
echo_broadcast "!!! We need to stop here"
teardown_environment_reverse
write_failure
exit 2
fi
echo_broadcast "====> Source identified: [${source}]"
echo_broadcast "====> Destination identified: [${destination}]"
echo_broadcast "====> Unmounting auto-mounted partitions"
NUM_MOUNTS=$(mount | grep -v none | grep "${destination}" | wc -l)
i=0 ; while test $i -le ${NUM_MOUNTS} ; do
DRIVE=$(mount | grep -v none | grep "${destination}" | tail -1 | awk '{print $1}')
umount ${DRIVE} >/dev/null 2>&1 || true
i=$(($i+1))
done
echo_broadcast "==> Figuring out machine"
get_device
echo_broadcast "====> Machine is ${machine}"
if [ "x${is_bbb}" = "xenable" ] ; then
echo_broadcast "====> Machine is compatible with BeagleBone Black"
fi
generate_line 80 '='
}
teardown_environment() {
generate_line 80 '='
echo_broadcast "Tearing Down script environment"
echo_broadcast "==> Unmounting /tmp"
flush_cache
umount /tmp || true
if [ ! "x${boot_drive}" = "x${root_drive}" ] ; then
echo_broadcast "==> Unmounting /boot"
flush_cache
umount /boot || true
fi
reset_leds 'none'
echo_broadcast "==> Force writeback of eMMC buffers by Syncing: ${destination}"
sync
generate_line 40
dd if=${destination} of=/dev/null count=100000
generate_line 40
echo_broadcast "===> Syncing: ${destination} complete"
end_time=$(date +%s)
echo_broadcast "==> This script took $((${end_time}-${start_time})) seconds to run"
generate_line 80 '='
}
teardown_environment_reverse() {
generate_line 80 '='
echo_broadcast "Tearing Down script environment"
flush_cache
reset_leds 'none'
echo_broadcast "==> Force writeback of eMMC buffers by Syncing: ${destination}"
sync
generate_line 40
dd if=${destination} of=/dev/null count=100000
generate_line 40
echo_broadcast "===> Syncing: ${destination} complete"
end_time=$(date +%s)
echo_broadcast "==> This script took $((${end_time}-${start_time})) seconds to run"
generate_line 80 '='
}
end_script() {
empty_line
if [ -f /boot/debug.txt ] ; then
echo_broadcast "This script has now completed its task"
generate_line 40
echo_broadcast "debug: enabled"
inf_loop
else
reset_leds 'default-on'
echo_broadcast '==> Displaying mount points'
generate_line 80
mount
generate_line 80
empty_line
generate_line 80 '='
echo_broadcast "eMMC has been flashed: please wait for device to power down."
generate_line 80 '='
flush_cache
unset are_we_flasher
are_we_flasher=$(grep init-eMMC-flasher /proc/cmdline || true)
if [ ! "x${are_we_flasher}" = "x" ] ; then
echo_broadcast "We are init"
#When run as init
exec /sbin/init
exit #We should not hit that
fi
echo_broadcast "Calling shutdown"
systemctl poweroff || halt
fi
}
check_if_run_as_root(){
if ! id | grep -q root; then
echo "must be run as root"
exit
fi
}
find_root_drive(){
unset root_drive
if [ -f /proc/cmdline ] ; then
proc_cmdline=$(cat /proc/cmdline | tr -d '\000')
echo_broadcast "==> ${proc_cmdline}"
generate_line 40
root_drive=$(cat /proc/cmdline | tr -d '\000' | sed 's/ /\n/g' | grep root=UUID= | awk -F 'root=' '{print $2}' || true)
if [ ! "x${root_drive}" = "x" ] ; then
root_drive=$(/sbin/findfs ${root_drive} || true)
else
root_drive=$(cat /proc/cmdline | sed 's/ /\n/g' | grep root= | awk -F 'root=' '{print $2}' || true)
fi
echo_broadcast "==> root_drive=[${root_drive}]"
else
echo_broadcast "no /proc/cmdline"
fi
}
flush_cache() {
sync
blockdev --flushbufs ${destination}
}
broadcast() {
if [ "x${message}" != "x" ] ; then
echo "${message}"
echo "${message}" > /dev/tty0 || true
fi
}
echo_broadcast() {
local _message="$1"
if [ "x${_message}" != "x" ] ; then
echo "${_message}"
echo "${_message}" > /dev/tty0 || true
fi
}
echo_debug() {
local _message="$1"
if [ "x${_message}" != "x" ] ; then
echo "${_message}" >&2
echo "${_message}" >&2 > /dev/tty0 || true
fi
}
empty_line() {
echo ""
echo "" > /dev/tty0 || true
}
generate_line() {
local line_length=${1:-80}
local line_char=${2:-\-}
local line=$(printf "%${line_length}s\n" | tr ' ' \\$line_char)
echo_broadcast "${line}"
}
inf_loop() {
while read MAGIC ; do
case $MAGIC in
beagleboard.org)
echo "Your foo is strong!"
bash -i
;;
*) echo "Your foo is weak."
;;
esac
done
}
# umount does not like device names without a valid /etc/mtab
# find the mount point from /proc/mounts
dev2dir() {
grep -m 1 '^$1 ' /proc/mounts | while read LINE ; do set -- $LINE ; echo $2 ; done
}
get_device() {
is_bbb="enable"
machine=$(cat /proc/device-tree/model | sed "s/ /_/g" | tr -d '\000')
case "${machine}" in
TI_AM5728_BeagleBoard*)
unset is_bbb
;;
esac
}
reset_leds() {
local leds_pattern0=${1:-heartbeat}
local leds_pattern1=${1:-mmc0}
local leds_pattern2=${1:-none}
local leds_pattern3=${1:-mmc1}
local leds_base=/sys/class/leds/beaglebone\:green\:usr
if [ "x${is_bbb}" = "xenable" ] ; then
if [ -e /proc/$CYLON_PID ]; then
echo_broadcast "==> Stopping Cylon LEDs ..."
kill $CYLON_PID > /dev/null 2>&1
fi
if [ -e ${leds_base}0/trigger ] ; then
echo_broadcast "==> Setting LEDs to ${leds_pattern}"
echo $leds_pattern0 > ${leds_base}0/trigger
echo $leds_pattern1 > ${leds_base}1/trigger
echo $leds_pattern2 > ${leds_base}2/trigger
echo $leds_pattern3 > ${leds_base}3/trigger
fi
else
echo_broadcast "!==> We don't know how to reset the leds as we are not a BBB compatible device"
fi
}
write_failure() {
echo_broadcast "writing to [${destination}] failed..."
reset_leds 'heartbeat'
generate_line 40
flush_cache
umount $(dev2dir ${destination}p1) > /dev/null 2>&1 || true
umount $(dev2dir ${destination}p2) > /dev/null 2>&1 || true
inf_loop
}
do_we_have_eeprom() {
unset got_eeprom
#v8 of nvmem...
if [ -f /sys/bus/nvmem/devices/at24-0/nvmem ] && [ "x${got_eeprom}" = "x" ] ; then
eeprom="/sys/bus/nvmem/devices/at24-0/nvmem"
eeprom_location="/sys/devices/platform/ocp/44e0b000.i2c/i2c-0/0-0050/at24-0/nvmem"
got_eeprom="true"
fi
#pre-v8 of nvmem...
if [ -f /sys/class/nvmem/at24-0/nvmem ] && [ "x${got_eeprom}" = "x" ] ; then
eeprom="/sys/class/nvmem/at24-0/nvmem"
eeprom_location="/sys/devices/platform/ocp/44e0b000.i2c/i2c-0/0-0050/nvmem/at24-0/nvmem"
got_eeprom="true"
fi
#eeprom 3.8.x & 4.4 with eeprom-nvmem patchset...
if [ -f /sys/bus/i2c/devices/0-0050/eeprom ] && [ "x${got_eeprom}" = "x" ] ; then
eeprom="/sys/bus/i2c/devices/0-0050/eeprom"
if [ -f /sys/devices/platform/ocp/44e0b000.i2c/i2c-0/0-0050/eeprom ] ; then
eeprom_location="/sys/devices/platform/ocp/44e0b000.i2c/i2c-0/0-0050/eeprom"
else
eeprom_location=$(ls /sys/devices/ocp*/44e0b000.i2c/i2c-0/0-0050/eeprom 2> /dev/null)
fi
got_eeprom="true"
fi
}
do_we_have_am335x_eeprom() {
do_we_have_eeprom
}
check_am335x_eeprom() {
empty_line
generate_line 40 '='
echo_broadcast "Checking for Valid ${device_eeprom} header"
do_we_have_am335x_eeprom
if [ "x${is_bbb}" = "xenable" ] ; then
if [ "x${got_eeprom}" = "xtrue" ] ; then
eeprom_header=$(hexdump -e '8/1 "%c"' ${eeprom} -n 8 | cut -b 6-8)
if [ "x${eeprom_header}" = "x335" ] ; then
echo_broadcast "==> Valid ${device_eeprom} header found [${eeprom_header}]"
generate_line 40 '='
else
echo_broadcast "==> Invalid EEPROM header detected"
if [ -f /opt/scripts/device/bone/${device_eeprom}.dump ] ; then
if [ ! "x${eeprom_location}" = "x" ] ; then
echo_broadcast "===> Writing header to EEPROM"
dd if=/opt/scripts/device/bone/${device_eeprom}.dump of=${eeprom_location}
sync
sync
eeprom_check=$(hexdump -e '8/1 "%c"' ${eeprom} -n 8 | cut -b 6-8)
echo_broadcast "===> eeprom check: [${eeprom_check}]"
generate_line 40 '='
#We have to reboot, as the kernel only loads the eMMC cape
# with a valid header
reboot -f
#We shouldnt hit this...
exit
fi
else
echo_broadcast "!==> error: no [/opt/scripts/device/bone/${device_eeprom}.dump]"
generate_line 40 '='
fi
fi
fi
fi
}
check_eeprom() {
check_am335x_eeprom
}
do_we_have_am57xx_eeprom() {
unset got_eeprom
if [ -f /sys/bus/i2c/devices/0-0050/eeprom ] && [ "x${got_eeprom}" = "x" ] ; then
eeprom="/sys/bus/i2c/devices/0-0050/eeprom"
if [ -f /sys/devices/platform/44000000.ocp/48070000.i2c/i2c-0/0-0050/eeprom ] ; then
eeprom_location="/sys/devices/platform/44000000.ocp/48070000.i2c/i2c-0/0-0050/eeprom"
fi
got_eeprom="true"
fi
}
check_am57xx_eeprom() {
empty_line
generate_line 40 '='
echo_broadcast "Checking for Valid ${device_eeprom} header"
do_we_have_am57xx_eeprom
if [ "x${got_eeprom}" = "xtrue" ] ; then
eeprom_header=$(hexdump -e '8/1 "%c"' ${eeprom} -n 3 | cut -b 2-3)
if [ "x${eeprom_header}" = "xU3" ] ; then
echo_broadcast "==> Valid ${device_eeprom} header found [${eeprom_header}]"
generate_line 40 '='
else
echo_broadcast "==> Invalid EEPROM header detected"
if [ -f /opt/scripts/device/${device_eeprom}.dump ] ; then
if [ ! "x${eeprom_location}" = "x" ] ; then
echo_broadcast "===> Writing header to EEPROM"
dd if=/opt/scripts/device/${device_eeprom}.dump of=${eeprom_location}
sync
sync
eeprom_check=$(hexdump -e '8/1 "%c"' ${eeprom} -n 3 | cut -b 2-3)
echo_broadcast "===> eeprom check: [${eeprom_check}]"
generate_line 40 '='
#We have to reboot, as the kernel only loads the eMMC cape
# with a valid header
reboot -f
#We shouldnt hit this...
exit
fi
else
echo_broadcast "!==> error: no [/opt/scripts/device/${device_eeprom}.dump]"
generate_line 40 '='
fi
fi
fi
}
countdown() {
local from_time=${1:-10}
while [ $from_time -gt 0 ] ; do
echo -n "${from_time} "
sleep 1
: $((from_time--))
done
empty_line
}
check_running_system() {
empty_line
generate_line 80 '='
echo_broadcast "Checking running system"
echo_broadcast "==> Copying: [${source}] -> [${destination}]"
echo_broadcast "==> lsblk:"
generate_line 40
echo_broadcast "`lsblk || true`"
generate_line 40
echo_broadcast "==> df -h | grep rootfs:"
echo_broadcast "`df -h | grep rootfs || true`"
generate_line 40
if [ ! -b "${destination}" ] ; then
echo_broadcast "!==> Error: [${destination}] does not exist"
write_failure
fi
if [ "x${is_bbb}" = "xenable" ] ; then
if [ ! -e /sys/class/leds/beaglebone\:green\:usr0/trigger ] ; then
modprobe leds_gpio || true
sleep 1
fi
fi
echo_broadcast "==> Giving you time to check..."
countdown 10
generate_line 80 '='
}
check_running_system_initrd() {
empty_line
generate_line 80 '='
echo_broadcast "Checking running system"
echo_broadcast "==> Copying: [${source}] -> [${destination}]"
echo_broadcast "==> lsblk:"
generate_line 40
echo_broadcast "`lsblk || true`"
generate_line 40
echo_broadcast "==> df -h | grep rootfs:"
echo_broadcast "`df -h | grep rootfs || true`"
generate_line 40
if [ ! -b "${destination}" ] ; then
echo_broadcast "!==> Error: [${destination}] does not exist"
write_failure
fi
if [ ! -f /boot/config-$(uname -r) ] ; then
echo_broadcast "==> generating: /boot/config-$(uname -r)"
zcat /proc/config.gz > /boot/config-$(uname -r)
fi
#Needed for: debian-7.5-2014-05-14
if [ ! -f /boot/vmlinuz-$(uname -r) ] ; then
echo_broadcast "==> updating: /boot/vmlinuz-$(uname -r) (old image)"
if [ -f /boot/uboot/zImage ] ; then
cp -v /boot/uboot/zImage /boot/vmlinuz-$(uname -r)
else
echo_broadcast "!==> Error: [/boot/vmlinuz-$(uname -r)] does not exist"
write_failure
fi
flush_cache
fi
if [ -f /boot/initrd.img-$(uname -r) ] ; then
echo_broadcast "==> updating: /boot/initrd.img-$(uname -r)"
update-initramfs -u -k $(uname -r)
else
echo_broadcast "==> creating: /boot/initrd.img-$(uname -r)"
update-initramfs -c -k $(uname -r)
fi
flush_cache
#Needed for: debian-7.5-2014-05-14
if [ ! -d /boot/dtbs/$(uname -r)/ ] ; then
if [ -d /boot/uboot/dtbs/ ] ; then
mkdir -p /boot/dtbs/$(uname -r) || true
cp -v /boot/uboot/dtbs/* /boot/dtbs/$(uname -r)/
else
echo_broadcast "!==> Error: [/boot/dtbs/$(uname -r)/] does not exist"
write_failure
fi
flush_cache
fi
if [ "x${is_bbb}" = "xenable" ] ; then
if [ ! -e /sys/class/leds/beaglebone\:green\:usr0/trigger ] ; then
modprobe leds_gpio || true
sleep 1
fi
fi
echo_broadcast "==> Giving you time to check..."
countdown 10
generate_line 80 '='
}
cylon_leds() {
if [ "x${is_bbb}" = "xenable" ] ; then
if [ -e /sys/class/leds/beaglebone\:green\:usr0/trigger ] ; then
BASE=/sys/class/leds/beaglebone\:green\:usr
echo none > ${BASE}0/trigger
echo none > ${BASE}1/trigger
echo none > ${BASE}2/trigger
echo none > ${BASE}3/trigger
STATE=1
while : ; do
case $STATE in
1)
echo 255 > ${BASE}0/brightness
echo 0 > ${BASE}1/brightness
STATE=2
;;
2)
echo 255 > ${BASE}1/brightness
echo 0 > ${BASE}0/brightness
STATE=3
;;
3)
echo 255 > ${BASE}2/brightness
echo 0 > ${BASE}1/brightness
STATE=4
;;
4)
echo 255 > ${BASE}3/brightness
echo 0 > ${BASE}2/brightness
STATE=5
;;
5)
echo 255 > ${BASE}2/brightness
echo 0 > ${BASE}3/brightness
STATE=6
;;
6)
echo 255 > ${BASE}1/brightness
echo 0 > ${BASE}2/brightness
STATE=1
;;
*)
echo 255 > ${BASE}0/brightness
echo 0 > ${BASE}1/brightness
STATE=2
;;
esac
sleep 0.1
done
fi
fi
}
_build_uboot_spl_dd_options() {
echo_broadcast "==> Figuring out options for SPL U-Boot copy ..."
unset dd_spl_uboot
if [ ! "x${dd_spl_uboot_count}" = "x" ] ; then
dd_spl_uboot="${dd_spl_uboot}count=${dd_spl_uboot_count} "
fi
if [ ! "x${dd_spl_uboot_seek}" = "x" ] ; then
dd_spl_uboot="${dd_spl_uboot}seek=${dd_spl_uboot_seek} "
fi
if [ ! "x${dd_spl_uboot_conf}" = "x" ] ; then
dd_spl_uboot="${dd_spl_uboot}conv=${dd_spl_uboot_conf} "
fi
if [ ! "x${dd_spl_uboot_bs}" = "x" ] ; then
dd_spl_uboot="${dd_spl_uboot}bs=${dd_spl_uboot_bs}"
fi
echo_broadcast "===> Will use : $dd_spl_uboot"
}
_build_uboot_dd_options() {
echo_broadcast "==> Figuring out options for U-Boot copy ..."
unset dd_uboot
if [ ! "x${dd_uboot_count}" = "x" ] ; then
dd_uboot="${dd_uboot}count=${dd_uboot_count} "
fi
if [ ! "x${dd_uboot_seek}" = "x" ] ; then
dd_uboot="${dd_uboot}seek=${dd_uboot_seek} "
fi
if [ ! "x${dd_uboot_conf}" = "x" ] ; then
dd_uboot="${dd_uboot}conv=${dd_uboot_conf} "
fi
if [ ! "x${dd_uboot_bs}" = "x" ] ; then
dd_uboot="${dd_uboot}bs=${dd_uboot_bs}"
fi
echo_broadcast "===> Will use : $dd_uboot"
}
_dd_bootloader() {
empty_line
generate_line 80 '='
echo_broadcast "Writing bootloader to [${destination}]"
generate_line 40
_build_uboot_spl_dd_options
_build_uboot_dd_options
echo_broadcast "==> Copying SPL U-Boot with dd if=${dd_spl_uboot_backup} of=${destination} ${dd_spl_uboot}"
generate_line 60
dd if=${dd_spl_uboot_backup} of=${destination} ${dd_spl_uboot}
generate_line 60
echo_broadcast "==> Copying U-Boot with dd if=${dd_uboot_backup} of=${destination} ${dd_uboot}"
generate_line 60
dd if=${dd_uboot_backup} of=${destination} ${dd_uboot}
generate_line 60
echo_broadcast "Writing bootloader completed"
generate_line 80 '='
}
_format_boot() {
empty_line
echo_broadcast "==> Formatting boot partition with mkfs.vfat -F 16 ${boot_partition} -n ${boot_label}"
generate_line 80
LC_ALL=C mkfs.vfat -c -F 16 ${boot_partition} -n ${boot_label}
generate_line 80
echo_broadcast "==> Formatting boot: ${boot_partition} complete"
flush_cache
}
_format_root() {
empty_line
echo_broadcast "==> Formatting rootfs with mkfs.ext4 -F ${ext4_options} ${rootfs_partition} -L ${rootfs_label}"
generate_line 80
empty_line
LC_ALL=C mkfs.ext4 -F ${ext4_options} ${rootfs_partition} -L ${rootfs_label}
generate_line 80
echo_broadcast "==> Formatting rootfs: ${rootfs_partition} complete"
flush_cache
}
_copy_boot() {
empty_line
generate_line 80 '='
echo_broadcast "Copying boot: ${source}p1 -> ${boot_partition}"
#rcn-ee: Currently the MLO/u-boot.img are dd'ed to MBR by default, this is just for VERY old rootfs (aka, DO NOT USE)
if [ ! -f /opt/backup/uboot/MLO ] ; then
if [ -f /boot/uboot/MLO ] && [ -f /boot/uboot/u-boot.img ] ; then
echo_broadcast "==> Found MLO and u-boot.img in current /boot/uboot/, copying"
#Make sure the BootLoader gets copied first:
cp -v /boot/uboot/MLO ${tmp_boot_dir}/MLO || write_failure
flush_cache
cp -v /boot/uboot/u-boot.img ${tmp_boot_dir}/u-boot.img || write_failure
flush_cache
fi
fi
if [ -f /boot/uboot/MLO ] ; then
echo_broadcast "==> rsync: /boot/uboot/ -> ${tmp_boot_dir}"
get_rsync_options
rsync -aAxv $rsync_options /boot/uboot/* ${tmp_boot_dir} --exclude={MLO,u-boot.img,uEnv.txt} || write_failure
flush_cache
empty_line
generate_line 80 '='
fi
}
get_device_uuid() {
local device=${1}
unset device_uuid
device_uuid=$(/sbin/blkid /dev/null -s UUID -o value ${device})
if [ ! -z "${device_uuid}" ] ; then
echo_debug "Device UUID should be: ${device_uuid}"
echo $device_uuid
else
echo_debug "Could not get a proper UUID for $device. Try with another ID"
fi
}
_generate_uEnv() {
local uEnv_file=${1:-${tmp_rootfs_dir}/boot/uEnv.txt}
empty_line
if [ -f $uEnv_file ]; then
echo_broadcast "==> Found pre-existing uEnv file at ${uEnv_file}. Using it."
generate_line 80 '*'
cat $uEnv_file
generate_line 80 '*'
empty_line
else
echo_broadcast "==> Could not find pre-existing uEnv file at ${uEnv_file}"
echo_broadcast "===> Generating it from template ..."
generate_line 40 '*'
cat > ${uEnv_file} <<__EOF__
#Docs: http://elinux.org/Beagleboard:U-boot_partitioning_layout_2.0
uname_r=$(uname -r)
#uuid=
#dtb=
cmdline=coherent_pool=1M quiet cape_universal=enable
##enable Generic eMMC Flasher:
##make sure, these tools are installed: dosfstools rsync
#cmdline=init=/opt/scripts/tools/eMMC/init-eMMC-flasher-v3.sh
__EOF__
flush_cache
generate_line 40 '*'
empty_line
fi
#UUID support for 3.8.x kernel
if [ -d /sys/devices/bone_capemgr.*/ ] ; then
root_uuid=$(get_device_uuid ${rootfs_partition})
if [ ! -z "${root_uuid}" ] ; then
echo_broadcast "==> Put root uuid in uEnv.txt"
sed -i -e 's:^uuid=:#uuid=:g' ${tmp_rootfs_dir}/boot/uEnv.txt
echo "uuid=${root_uuid}" >> ${tmp_rootfs_dir}/boot/uEnv.txt
fi
fi
}
get_fstab_id_for_device() {
local device=${1}
local device_id=$(get_device_uuid ${device})
if [ -n ${device_id} ]; then
echo "UUID=${device_id}"
else
echo_debug "Could not find a UUID for ${device}, default to device name"
# Since the mmc device get reverse depending on how it was booted we need to use source
echo "${source}p${device:(-1)}"