-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash-SDCard-from-eMMC.sh
executable file
·211 lines (174 loc) · 5.95 KB
/
flash-SDCard-from-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
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
#!/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}
}
write_failure () {
echo "writing to [${destination}] failed..."
echo "-----------------------------"
flush_cache
umount ${destination}p1 > /dev/null 2>&1 || true
umount ${destination}p2 > /dev/null 2>&1 || true
}
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
}
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} || 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
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"
else
echo "Shutting Down"
umount /tmp || umount -l /tmp
mount
echo ""
echo "-----------------------------"
echo ""
echo "eMMC has been flashed, please remove power and microSD card"
echo ""
echo "-----------------------------"
halt -f
fi
}
check_running_system
partition_drive
copy_boot
copy_rootfs