forked from friendlyarm/sd-fuse_h5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fusing.sh
executable file
·198 lines (167 loc) · 4.83 KB
/
fusing.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
#!/bin/bash
set -eu
# Copyright (C) Guangzhou FriendlyARM Computer Tech. Co., Ltd.
# (http://www.friendlyarm.com)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can access it online at
# http://www.gnu.org/licenses/gpl-2.0.html.
# ----------------------------------------------------------
# Checking device for fusing
if [ $# -eq 0 ]; then
echo "Usage: $0 DEVICE <friendlycore-xenial_4.14_arm64 | friendlywrt_4.14_arm64 | eflasher>"
exit 0
fi
case $1 in
/dev/sd[a-z] | /dev/loop[0-9]* | /dev/mmcblk[0-9]*)
if [ ! -e $1 ]; then
echo "Error: $1 does not exist."
exit 1
fi
DEV_NAME=`basename $1`
BLOCK_CNT=`cat /sys/block/${DEV_NAME}/size` ;;&
/dev/sd[a-z])
DEV_PART=${DEV_NAME}3
REMOVABLE=`cat /sys/block/${DEV_NAME}/removable` ;;
/dev/mmcblk[0-9]* | /dev/loop[0-9]*)
DEV_PART=${DEV_NAME}p3
REMOVABLE=1 ;;
*)
echo "Error: Unsupported SD reader"
exit 0
esac
if [ ${REMOVABLE} -le 0 ]; then
echo "Error: $1 is a non-removable device. Stop."
exit 1
fi
if [ -z ${BLOCK_CNT} -o ${BLOCK_CNT} -le 0 ]; then
echo "Error: $1 is inaccessible. Stop fusing now!"
exit 1
fi
let DEV_SIZE=${BLOCK_CNT}/2
if [ ${DEV_SIZE} -gt 64000000 ]; then
echo "Error: $1 size (${DEV_SIZE} KB) is too large"
exit 1
fi
true ${MIN_SIZE:=600000}
if [ ${DEV_SIZE} -le ${MIN_SIZE} ]; then
echo "Error: $1 size (${DEV_SIZE} KB) is too small"
echo " please try another SD card."
exit 1
fi
# ----------------------------------------------------------
# Get target OS
true ${TARGET_OS:=${2,,}}
PARTMAP=./${TARGET_OS}/partmap.txt
case ${TARGET_OS} in
friendlycore-xenial_4.14_arm64 | friendlywrt_4.14_arm64 | eflasher)
;;
*)
echo "Error: Unsupported target OS: ${TARGET_OS}"
exit 0
esac
if [[ ! -z ${TARGET_OS} && ! -f ${PARTMAP} ]]; then
ROMFILE=`./tools/get_pkg_filename.sh ${TARGET_OS}`
cat << EOF
Warn: Image not found for ${TARGET_OS}
----------------
you may download them from the netdisk (dl.friendlyarm.com) to get a higher downloading speed,
the image files are stored in a directory called images-for-eflasher, for example:
tar xvzf /path/to/NETDISK/images-for-eflasher/${ROMFILE}
----------------
Or, download from http (Y/N)?
EOF
while read -r -n 1 -t 3600 -s USER_REPLY; do
if [[ ${USER_REPLY} = [Nn] ]]; then
echo ${USER_REPLY}
exit 1
elif [[ ${USER_REPLY} = [Yy] ]]; then
echo ${USER_REPLY}
break;
fi
done
if [ -z ${USER_REPLY} ]; then
echo "Cancelled."
exit 1
fi
./tools/get_rom.sh ${TARGET_OS} || exit 1
fi
# Automatically re-run script under sudo if not root
if [ $(id -u) -ne 0 ]; then
echo "Re-running script under sudo..."
sudo "$0" "$@"
exit
fi
# ----------------------------------------------------------
# Get host machine
ARCH=
if grep 'ARMv7 Processor' /proc/cpuinfo >/dev/null; then
# EMMC=.emmc
ARCH=armv7/
fi
# ----------------------------------------------------------
# Fusing 2ndboot, bootloader to card
function fusing_bin() {
[ -z $2 -o ! -f $1 ] && return 1
echo "---------------------------------"
echo "$1 fusing"
echo "dd if=$1 of=/dev/${DEV_NAME} bs=512 seek=$2"
dd if=$1 of=/dev/${DEV_NAME} bs=512 seek=$2 conv=fdatasync
}
# umount all at first
set +e
umount /dev/${DEV_NAME}* > /dev/null 2>&1
set -e
# ----------------------------------------------------------
# partition card & fusing filesystem
true ${SD_UPDATE:=./tools/${ARCH}sd_update}
true ${SD_TUNEFS:=./tools/sd_tune2fs.sh}
[[ -z $2 && ! -f ${PARTMAP} ]] && {
echo "abort, args2 = $2, partmap = ${PARTMAP}"
exit 0
}
echo "---------------------------------"
echo "${TARGET_OS^} filesystem fusing"
echo "Image root: `dirname ${PARTMAP}`"
echo
if [ ! -f ${PARTMAP} ]; then
echo "Error: ${PARTMAP}: File not found"
echo " Stop fusing now"
exit 1
fi
# write ext4 image
${SD_UPDATE} -d /dev/${DEV_NAME} -p ${PARTMAP}
if [ $? -ne 0 ]; then
echo "Error: filesystem fusing failed, Stop."
exit 1
fi
if [ -z ${ARCH} ]; then
partprobe /dev/${DEV_NAME} -s 2>/dev/null
fi
if [ $? -ne 0 ]; then
echo "Warning: Re-reading the partition table failed"
else
# optional: update uuid & label
case ${TARGET_OS} in
android)
sleep 1
${SD_TUNEFS} /dev/${DEV_NAME};;
friendlycore* | friendlywrt* )
sleep 1
echo "### try to resize2fs: /dev/${DEV_PART}"
resize2fs -f /dev/${DEV_PART};;
esac
fi
echo "---------------------------------"
echo "${TARGET_OS^} is fused successfully."
echo "All done."