-
Notifications
You must be signed in to change notification settings - Fork 16
/
bela_flash_emmc.sh
executable file
·139 lines (121 loc) · 3.76 KB
/
bela_flash_emmc.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
#!/bin/bash
set -ex
[ -z "$FLASH_DESTINATION" ] && FLASH_DESTINATION=emmc
if [ "$FLASH_DESTINATION" == emmc ]; then
DEVICE=/dev/mmcblk1
MMCID=1
THIS_DEVICE=/dev/mmcblk0
elif [ "$FLASH_DESTINATION" == sd ]; then
DEVICE=/dev/mmcblk0
MMCID=0
THIS_DEVICE=/dev/mmcblk1
else
echo "Invalid \$FLASH_DESTINATION: '$FLASH_DESTINATION' should be either 'emmc' or 'sd'" >& 2
exit 1
fi
MNT_BOOT=`mktemp -d /tmp/${FLASH_DESTINATION}_boot.XXXXXX`
MNT_ROOT=`mktemp -d /tmp/${FLASH_DESTINATION}_root.XXXXXX`
MNT_THIS_BOOT=`mktemp -d /tmp/boot.XXXXX`
USR1=/sys/class/leds/beaglebone\:green\:usr1/trigger
USR2=/sys/class/leds/beaglebone\:green\:usr2/trigger
USR3=/sys/class/leds/beaglebone\:green\:usr3/trigger
SUCCESS=0
cleanup() {
for a in $MNT_BOOT $MNT_ROOT $MNT_THIS_BOOT; do
mountpoint -q $a && umount $a && rm -rf $a || true
done
}
final_check()
{
set +x
cleanup
if [ $SUCCESS -eq 0 ]
then
echo "An error occurred while flashing the $FLASH_DESTINATION" >& 2
while sleep 0.5
do
echo "default-on" > $USR1
echo "default-on" > $USR2
echo "default-on" > $USR3
sleep 0.5
echo "none" > $USR1
echo "none" > $USR2
echo "none" > $USR3
done
else
echo Flashing was successful
fi
}
trap final_check EXIT
# Stop the Bela program if currently running. Makes for a faster copy.
make --no-print-directory -C /root/Bela stop || true
echo "default-on" > $USR1
echo "default-on" > $USR2
echo "default-on" > $USR3
# test if we are trying to flash the device whose partition is currently mounted on /
mount | grep "${DEVICE}p2" | grep -q "on / " && {
echo "You are trying to flash $DEVICE ($FLASH_DESTINATION) but you have actually just
booted from it. Specify a different FLASH_DESTINATION ('sd' or 'emmc'),
whichever you have NOT booted from" >& 2
exit 1;
}
# just in case anything from the destination is mounted, unmount it
umount -A ${DEVICE}p1 || true
umount -A ${DEVICE}p2 || true
mount | grep "${DEVICE}" && {
echo "One or more filesystems on ${DEVICE} are busy and could not be
unmounted" >& 2
exit 1;
}
echo "
label: dos
label-id: 0x68a0906e
device: ${DEVICE}
unit: sectors
${DEVICE}p1 : start= 2048, size= 137953, type=c, bootable
${DEVICE}p2 : start= 141312, type=83
" | sfdisk $DEVICE
mkfs.vfat ${DEVICE}p1
mkfs.ext4 -F ${DEVICE}p2
dosfslabel ${DEVICE}p1 BELABOOT
e2label ${DEVICE}p2 BELAROOTFS
echo "copying files, this may take a few minutes..."
function mount_fat()
{
mount ${DEVICE}p1 $MNT_BOOT
mount ${THIS_DEVICE}p1 $MNT_THIS_BOOT
}
if [ $(lsb_release -rs) -ge 11 ]; then
# bullseye's cp command doesn't seem to copy files exactly the same as
# previous versions. dd, OTOH, does the job just fine (while dest and
# source are unmounted)
dd if=/dev/mmcblk0 of=/dev/mmcblk1 bs=512 seek=2048 skip=2048 count=137953
mount_fat
else
mount_fat
cp $MNT_THIS_BOOT/MLO $MNT_BOOT
sync
cp $MNT_THIS_BOOT/u-boot.img $MNT_BOOT
sync
rsync -r --exclude=$MNT_THIS_BOOT/MLO,$MNT_THIS_BOOT/u-boot.img,$MNT_THIS_BOOT/'FSCK*REC' $MNT_THIS_BOOT/* $MNT_BOOT
fi
cat $MNT_THIS_BOOT/uEnv.txt | sed "s/^\(\s*mmcid=\).*/\1$MMCID/" > $MNT_BOOT/uEnv.txt
umount $MNT_THIS_BOOT
rm -rf $MNT_THIS_BOOT
mount ${DEVICE}p2 $MNT_ROOT
for a in /boot/ /dev/ /etc/ /home/ /opt/ /root/ /srv/ /usr/ /var/; do
cp -a $a $MNT_ROOT
done
ln -s usr/bin $MNT_ROOT/bin
ln -s usr/lib $MNT_ROOT/lib
ln -s usr/sbin $MNT_ROOT/sbin
mkdir -p $MNT_ROOT/media $MNT_ROOT/mnt $MNT_ROOT/proc $MNT_ROOT/run/ $MNT_ROOT/sys $MNT_ROOT/tmp
cat /etc/fstab | sed "s:/dev/mmcblk1:/dev/mmcblkX:g" | sed "s:/dev/mmcblk0:/dev/mmcblk1:" | sed "s:/dev/mmcblkX:/dev/mmcblk0:" > $MNT_ROOT/etc/fstab
rm -rf $MNT_ROOT/etc/systemd/system/default.target.wants/bela_flash_emmc.service
rm -rf $MNT_ROOT/etc/cpsw* # reset mac address
sync
echo "Done!"
echo "mmc0" > $USR1
echo "none" > $USR2
echo "mmc1" > $USR3
SUCCESS=1