-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash-eMMC-from-SDCard.sh
executable file
·316 lines (270 loc) · 8.64 KB
/
flash-eMMC-from-SDCard.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
#!/bin/bash -e
#
# Copyright (c) 2013-2014 Robert Nelson <[email protected]>
# Portions copyright (c) 2014 Charles Steinkuehler <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#This script assumes, these packages are installed, as network may not be setup
#dosfstools initramfs-tools rsync u-boot-tools
if ! id | grep -q root; then
echo "must be run as root"
exit
fi
source="/dev/mmcblk0"
destination="/dev/mmcblk1"
flush_cache () {
sync
blockdev --flushbufs ${destination}
}
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
}
write_failure () {
echo "writing to [${destination}] failed..."
[ -e /proc/$CYLON_PID ] && kill $CYLON_PID > /dev/null 2>&1
if [ -e /sys/class/leds/beaglebone\:green\:usr0/trigger ] ; then
echo heartbeat > /sys/class/leds/beaglebone\:green\:usr0/trigger
echo heartbeat > /sys/class/leds/beaglebone\:green\:usr1/trigger
echo heartbeat > /sys/class/leds/beaglebone\:green\:usr2/trigger
echo heartbeat > /sys/class/leds/beaglebone\:green\:usr3/trigger
fi
echo "-----------------------------"
flush_cache
umount ${destination}p1 > /dev/null 2>&1 || true
umount ${destination}p2 > /dev/null 2>&1 || true
inf_loop
}
check_eeprom () {
eeprom="/sys/bus/i2c/devices/0-0050/eeprom"
#Flash BeagleBone Black's eeprom:
eeprom_location=$(ls /sys/devices/ocp*/44e0b000.i2c/i2c-0/0-0050/eeprom 2> /dev/null)
eeprom_header=$(hexdump -e '8/1 "%c"' ${eeprom} -s 5 -n 3)
if [ "x${eeprom_header}" = "x335" ] ; then
echo "Valid EEPROM header found"
else
echo "Invalid EEPROM header detected"
if [ -f /opt/scripts/device/bone/bbb-eeprom.dump ] ; then
if [ ! "x${eeprom_location}" = "x" ] ; then
echo "Adding header to EEPROM"
dd if=/opt/scripts/device/bone/bbb-eeprom.dump of=${eeprom_location}
sync
#We have to reboot, as the kernel only loads the eMMC cape
# with a valid header
reboot -f
#We shouldnt hit this...
exit
fi
fi
fi
}
check_running_system () {
echo "-----------------------------"
echo "debug copying: [${source}] -> [${destination}]"
lsblk
echo "-----------------------------"
if [ ! -b "${destination}" ] ; then
echo "Error: [${destination}] does not exist"
write_failure
fi
if [ ! -f /boot/config-$(uname -r) ] ; then
zcat /proc/config.gz > /boot/config-$(uname -r)
fi
if [ -f /boot/initrd.img-$(uname -r) ] ; then
update-initramfs -u -k $(uname -r)
else
update-initramfs -c -k $(uname -r)
fi
flush_cache
}
cylon_leds () {
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
}
format_boot () {
mkfs.vfat -F 16 ${destination}p1 -n BEAGLEBONE
flush_cache
}
format_root () {
mkfs.ext4 ${destination}p2 -L rootfs
flush_cache
}
partition_drive () {
echo "Erasing: ${destination}"
flush_cache
dd if=/dev/zero of=${destination} bs=1M count=20
sync
dd if=${destination} of=/dev/null bs=1M count=20
sync
flush_cache
if [ -f /boot/SOC.sh ] ; then
. /boot/SOC.sh
fi
conf_boot_startmb=${conf_boot_startmb:-"1"}
conf_boot_endmb=${conf_boot_endmb:-"12"}
sfdisk_fstype=${sfdisk_fstype:-"0xE"}
echo "Formatting: ${destination}"
#96Mb fat formatted boot partition
LC_ALL=C sfdisk --force --in-order --Linux --unit M "${destination}" <<-__EOF__
${conf_boot_startmb},${conf_boot_endmb},${sfdisk_fstype},*
,,,-
__EOF__
flush_cache
format_boot
format_root
}
copy_boot () {
mount ${source}p1 /boot/uboot -o ro
echo "Copying: ${source}p1 -> ${destination}p1"
mkdir -p /tmp/boot/ || true
mount ${destination}p1 /tmp/boot/ -o sync
#Make sure the BootLoader gets copied first:
cp -v /boot/uboot/MLO /tmp/boot/MLO || write_failure
flush_cache
cp -v /boot/uboot/u-boot.img /tmp/boot/u-boot.img || write_failure
flush_cache
echo "rsync: /boot/uboot/ -> /tmp/boot/"
rsync -aAX /boot/uboot/ /tmp/boot/ --exclude={MLO,u-boot.img,uEnv.txt} || write_failure
flush_cache
flush_cache
umount /tmp/boot/ || umount -l /tmp/boot/ || write_failure
flush_cache
umount /boot/uboot || umount -l /boot/uboot
}
copy_rootfs () {
echo "Copying: ${source}p2 -> ${destination}p2"
mkdir -p /tmp/rootfs/ || true
mount ${destination}p2 /tmp/rootfs/ -o async,noatime
echo "rsync: / -> /tmp/rootfs/"
rsync -aAX /* /tmp/rootfs/ --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/lib/modules/*} || write_failure
flush_cache
#ssh keys will now get regenerated on the next bootup
touch /tmp/rootfs/etc/ssh/ssh.regenerate
flush_cache
mkdir -p /tmp/rootfs/lib/modules/$(uname -r)/ || true
echo "Copying: Kernel modules"
echo "rsync: /lib/modules/$(uname -r)/ -> /tmp/rootfs/lib/modules/$(uname -r)/"
rsync -aAX /lib/modules/$(uname -r)/* /tmp/rootfs/lib/modules/$(uname -r)/ || write_failure
flush_cache
unset root_uuid
root_uuid=$(/sbin/blkid -c /dev/null -s UUID -o value ${destination}p2)
if [ "${root_uuid}" ] ; then
sed -i -e 's:uuid=:#uuid=:g' /tmp/rootfs/boot/uEnv.txt
echo "uuid=${root_uuid}" >> /tmp/rootfs/boot/uEnv.txt
root_uuid="UUID=${root_uuid}"
else
#really a failure...
root_uuid="${source}p2"
fi
echo "/boot/uEnv.txt: disabling flasher script"
script="cmdline=init=/opt/scripts/tools/eMMC/init-eMMC-flasher-v2.sh"
sed -i -e 's:'$script':#'$script':g' /tmp/rootfs/boot/uEnv.txt
cat /tmp/rootfs/boot/uEnv.txt
echo "Generating: /etc/fstab"
echo "# /etc/fstab: static file system information." > /tmp/rootfs/etc/fstab
echo "#" >> /tmp/rootfs/etc/fstab
echo "${root_uuid} / ext4 noatime,errors=remount-ro 0 1" >> /tmp/rootfs/etc/fstab
echo "debugfs /sys/kernel/debug debugfs defaults 0 0" >> /tmp/rootfs/etc/fstab
cat /tmp/rootfs/etc/fstab
flush_cache
umount /tmp/rootfs/ || umount -l /tmp/rootfs/ || write_failure
[ -e /proc/$CYLON_PID ] && kill $CYLON_PID
echo "Syncing: ${destination}"
#https://github.com/beagleboard/meta-beagleboard/blob/master/contrib/bone-flash-tool/emmc.sh#L158-L159
# force writeback of eMMC buffers
sync
dd if=${destination} of=/dev/null count=100000
echo ""
echo "This script has now completed its task"
echo "-----------------------------"
if [ -f /boot/debug.txt ] ; then
echo "debug: enabled"
inf_loop
else
echo "Shutting Down"
umount /tmp || umount -l /tmp
if [ -e /sys/class/leds/beaglebone\:green\:usr0/trigger ] ; then
echo default-on > /sys/class/leds/beaglebone\:green\:usr0/trigger
echo default-on > /sys/class/leds/beaglebone\:green\:usr1/trigger
echo default-on > /sys/class/leds/beaglebone\:green\:usr2/trigger
echo default-on > /sys/class/leds/beaglebone\:green\:usr3/trigger
fi
mount
echo ""
echo "-----------------------------"
echo ""
echo "eMMC has been flashed, please remove power and microSD card"
echo ""
echo "-----------------------------"
halt -f
fi
}
check_eeprom
check_running_system
cylon_leds & CYLON_PID=$!
partition_drive
copy_boot
copy_rootfs