-
Notifications
You must be signed in to change notification settings - Fork 13
/
mk-sd-image.sh
executable file
·221 lines (195 loc) · 5.41 KB
/
mk-sd-image.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
#!/bin/bash
set -eu
# Copyright (C) Guangzhou FriendlyElec Computer Tech. Co., Ltd.
# (http://www.friendlyelec.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.
function usage() {
echo "Usage: $0 <img dir>"
exit 0
}
if [ $# -eq 0 ]; then
usage
fi
. tools/util.sh
check_and_install_package
# ----------------------------------------------------------
# Get platform, target OS
true ${SOC:=rk3588}
true ${TARGET_OS:=$(echo ${1,,}|sed 's/\///g')}
# ----------------------------------------------------------
# Create zero file
RK_PARAMETER_TXT=$(dirname $0)/${TARGET_OS}/parameter.txt
true ${RAW_SIZE_MB:=0}
if [ $RAW_SIZE_MB -eq 0 ]; then
case ${TARGET_OS} in
android*)
RAW_SIZE_MB=7800 ;;
buildroot*)
RAW_SIZE_MB=7800 ;;
openmediavault-*)
RAW_SIZE_MB=7800 ;;
debian-*)
RAW_SIZE_MB=7800 ;;
ubuntu-*)
RAW_SIZE_MB=7800 ;;
friendlycore-*)
RAW_SIZE_MB=7800 ;;
friendlywrt*)
RAW_SIZE_MB=1500 ;;
eflasher)
RAW_SIZE_MB=7800 ;;
*)
RAW_SIZE_MB=7800 ;;
esac
fi
if [ $# -eq 2 ]; then
RAW_FILE=$2
else
case ${TARGET_OS} in
buildroot*|friendlycore-*|openmediavault-*|debian-*|ubuntu-*|android*)
RAW_FILE=${SOC}-sd-${TARGET_OS%-*}-6.1-arm64-$(date +%Y%m%d).img
;;
friendlywrt23)
RAW_FILE=${SOC}-sd-friendlywrt-23.05-arm64-$(date +%Y%m%d).img
;;
friendlywrt23-docker)
RAW_FILE=${SOC}-sd-friendlywrt-23.05-docker-arm64-$(date +%Y%m%d).img
;;
friendlywrt22)
RAW_FILE=${SOC}-sd-friendlywrt-22.03-arm64-$(date +%Y%m%d).img
;;
friendlywrt22-docker)
RAW_FILE=${SOC}-sd-friendlywrt-22.03-docker-arm64-$(date +%Y%m%d).img
;;
friendlywrt21)
RAW_FILE=${SOC}-sd-friendlywrt-21.02-arm64-$(date +%Y%m%d).img
;;
friendlywrt21-docker)
RAW_FILE=${SOC}-sd-friendlywrt-21.02-docker-arm64-$(date +%Y%m%d).img
;;
eflasher)
RAW_FILE=${SOC}-eflasher-$(date +%Y%m%d).img
;;
*)
RAW_FILE=${SOC}-sd-${TARGET_OS}-$(date +%Y%m%d).img
;;
esac
fi
# ----------------------------------------------------------
# Get target OS
if [ ! -f "${RK_PARAMETER_TXT}" ]; then
ROMFILE=`./tools/get_pkg_filename.sh ${TARGET_OS}`
cat << EOF
Warn: Image not found for ${1}
----------------
you may download it from the netdisk (dl.friendlyarm.com) to get a higher downloading speed,
the image files are stored in a directory called "03_Partition image files", for example:
tar xvzf /path/to/NetDrive/03_Partition\ image\ files/${ROMFILE}
----------------
Do you want to download it now via 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
# ----------------------------------------------------------
# Create zero file
OUT=out
if [ ! -d $OUT ]; then
echo "path not found: $PWD/$OUT"
exit 1
fi
RAW_FILE=${OUT}/${RAW_FILE}
if [ -f "${RAW_FILE}" ]; then
rm -f ${RAW_FILE}
fi
BLOCK_SIZE=1024
let RAW_SIZE=(${RAW_SIZE_MB}*1000*1000)/${BLOCK_SIZE}
echo "Creating RAW image: ${RAW_FILE} (${RAW_SIZE_MB} MB)"
echo "---------------------------------"
dd if=/dev/zero of=${RAW_FILE} bs=${BLOCK_SIZE} count=0 seek=${RAW_SIZE}
if [ $? -ne 0 ]; then
echo "Error: ${RAW_FILE}: Creating RAW file failed"
exit 1
fi
# ----------------------------------------------------------
# Fusing all
if [ "x${TARGET_OS}" = "xeflasher" ]; then
true ${SD_FUSING:=$(dirname $0)/fusing.sh}
# Automatically re-run script under sudo if not root
if [ $(id -u) -ne 0 ]; then
echo "Re-running script under sudo..."
sudo --preserve-env "$0" "$@"
exit
fi
# ----------------------------------------------------------
# Setup loop device
LOOP_DEVICE=$(losetup -f)
echo "Using device: ${LOOP_DEVICE}"
for i in `seq 3`; do
if [ -b ${LOOP_DEVICE} ]; then
break
else
echo "Waitting ${LOOP_DEVICE}"
sleep 1
fi
done
if losetup ${LOOP_DEVICE} ${RAW_FILE}; then
USE_KPARTX=1
PART_DEVICE=/dev/mapper/`basename ${LOOP_DEVICE}`
sleep 1
else
echo "Error: attaching ${LOOP_DEVICE} failed, stop now."
rm ${RAW_FILE}
exit 1
fi
RK_PARAMETER_TXT=${RK_PARAMETER_TXT} ${SD_FUSING} ${LOOP_DEVICE} ${TARGET_OS}
RET=$?
if [ ${RET} -ne 0 ]; then
echo "Error: ${RAW_FILE}: Fusing image failed, cleanup"
rm -f ${RAW_FILE}
exit 1
fi
sudo mkfs.exfat ${LOOP_DEVICE}p1 -n FriendlyARM
# cleanup
losetup -d ${LOOP_DEVICE}
else
HOST_ARCH=
if uname -mpi | grep aarch64 >/dev/null; then
HOST_ARCH="aarch64/"
fi
true ${SD_UPDATE:=$(dirname $0)/tools/${HOST_ARCH}sd_update}
${SD_UPDATE} -d ${RAW_FILE} -p ${RK_PARAMETER_TXT}
if [ $? -ne 0 ]; then
echo "Error: filesystem fusing failed, Stop."
exit 1
fi
fi
echo "---------------------------------"
echo "RAW image successfully created (`date +%T`)."
ls -l ${RAW_FILE}
echo "Tip: You can compress it to save disk space."