-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsd_fusing_rpi3.sh
executable file
·359 lines (302 loc) · 7.44 KB
/
sd_fusing_rpi3.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/bash
declare FORMAT=""
declare DEVICE=""
# Binaires array for fusing
declare -a FUSING_BINARY_ARRAY
declare -i FUSING_BINARY_NUM=0
declare CONV_ASCII=""
declare -i FUS_ENTRY_NUM=0
# binary name | part number | bs
declare -a PART_TABLE=(
"boot.img" 1 512
"rootfs.img" 2 4M
"system-data.img" 3 4M
"user.img" 5 4M
"modules.img" 6 512
"ramdisk.img" 7 512
"ramdisk-recovery.img" 8 512
"inform.img" 9 512
)
declare -r -i PART_TABLE_ROW=3
declare -r -i PART_TABLE_COL=${#PART_TABLE[*]}/${PART_TABLE_ROW}
# partition table support
function get_index_use_name () {
local -r binary_name=$1
for ((idx=0;idx<$PART_TABLE_COL;idx++)); do
if [ ${PART_TABLE[idx * ${PART_TABLE_ROW} + 0]} == $binary_name ]; then
return $idx
fi
done
# return out of bound index
return $idx
}
function print_message () {
local color=$1
local message=$2
tput setaf $color
tput bold
echo ""
echo $message
tput sgr 0
}
function fusing_image () {
local -r fusing_img=$1
# get binary info using basename
get_index_use_name $(basename $fusing_img)
local -r -i part_idx=$?
local EFFECTIVE_DEVICE=$DEVICE
if [[ "$DEVICE" =~ [0-9]$ ]]
then
EFFECTIVE_DEVICE+="p"
fi
if [ $part_idx -ne $PART_TABLE_COL ];then
local -r device=$EFFECTIVE_DEVICE${PART_TABLE[${part_idx} * ${PART_TABLE_ROW} + 1]}
local -r bs=${PART_TABLE[${part_idx} * ${PART_TABLE_ROW} + 2]}
else
echo "Not supported binary: $fusing_img"
return
fi
local -r input_size=`du -b $fusing_img | awk '{print $1}'`
print_message 2 "[Fusing $1]"
umount $device
dd if=$fusing_img | pv -s $input_size | dd of=$device bs=$bs
resize2fs -f $device
}
function fuse_image_tarball () {
local -r filepath=$1
local -r temp_dir="tar_tmp"
mkdir -p $temp_dir
tar xvf $filepath -C $temp_dir
cd $temp_dir
for file in *
do
fusing_image $file
done
cd ..
rm -rf $temp_dir
eval sync
}
function fuse_image () {
if [ "$FUSING_BINARY_NUM" == 0 ]; then
return
fi
for ((fuse_idx = 0 ; fuse_idx < $FUSING_BINARY_NUM ; fuse_idx++))
do
local filename=${FUSING_BINARY_ARRAY[fuse_idx]}
case "$filename" in
*.tar | *.tar.gz)
fuse_image_tarball $filename
;;
*)
fusing_image $filename
;;
esac
done
echo ""
}
# partition format
function mkpart_3 () {
# NOTE: if your sfdisk version is less than 2.26.0, then you should use following sfdisk command:
# sfdisk --in-order --Linux --unit M $DISK <<-__EOF__
# NOTE: sfdisk 2.26 doesn't support units other than sectors and marks --unit option as deprecated.
# The input data needs to contain multipliers (MiB) instead.
local version=`sfdisk -v | awk '{print $4}'`
local major=${version%%.*}
local version=${version:`expr index $version .`}
local minor=${version%%.*}
local sfdisk_new=0
if [ $major -gt 2 ]; then
sfdisk_new=1
else
if [ $major -eq 2 -a $minor -ge 26 ]; then
sfdisk_new=1
fi
fi
local -r DISK=$DEVICE
local -r SIZE=`sfdisk -s $DISK`
local -r SIZE_MB=$((SIZE >> 10))
local -r BOOT_SZ=64
local -r ROOTFS_SZ=3072
local -r DATA_SZ=512
local -r MODULE_SZ=20
local -r RAMDISK_SZ=8
local -r RAMDISK_RECOVERY_SZ=12
local -r INFORM_SZ=8
if [ $sfdisk_new == 1 ]; then
local -r EXTEND_SZ=8
else
local -r EXTEND_SZ=4
fi
let "USER_SZ = $SIZE_MB - $BOOT_SZ - $ROOTFS_SZ - $DATA_SZ - $MODULE_SZ - $RAMDISK_SZ - $RAMDISK_RECOVERY_SZ - $INFORM_SZ - $EXTEND_SZ"
local -r BOOT=boot
local -r ROOTFS=rootfs
local -r SYSTEMDATA=system-data
local -r USER=user
local -r MODULE=modules
local -r RAMDISK=ramdisk
local -r RAMDISK_RECOVERY=ramdisk-recovery
local -r INFORM=inform
local EFFECTIVE_DISK=$DISK
if [[ "$DISK" =~ [0-9]$ ]]
then
EFFECTIVE_DISK+="p"
fi
if [[ $USER_SZ -le 100 ]]
then
echo "We recommend to use more than 4GB disk"
exit 0
fi
echo "========================================"
echo "Label dev size"
echo "========================================"
echo $BOOT" " $EFFECTIVE_DISK"1 " $BOOT_SZ "MB"
echo $ROOTFS" " $EFFECTIVE_DISK"2 " $ROOTFS_SZ "MB"
echo $SYSTEMDATA" " $EFFECTIVE_DISK"3 " $DATA_SZ "MB"
echo "[Extend]"" " $EFFECTIVE_DISK"4"
echo " "$USER" " $EFFECTIVE_DISK"5 " $USER_SZ "MB"
echo " "$MODULE" " $EFFECTIVE_DISK"6 " $MODULE_SZ "MB"
echo " "$RAMDISK" " $EFFECTIVE_DISK"7 " $RAMDISK_SZ "MB"
echo " "$RAMDISK_RECOVERY" " $EFFECTIVE_DISK"8 " $RAMDISK_RECOVERY_SZ "MB"
echo " "$INFORM" " $EFFECTIVE_DISK"9 " $INFORM_SZ "MB"
local MOUNT_LIST=`mount | grep $DISK | awk '{print $1}'`
for mnt in $MOUNT_LIST
do
umount $mnt
done
echo "Remove partition table..."
dd if=/dev/zero of=$DISK bs=512 count=16 conv=notrunc
if [ $sfdisk_new == 1 ]; then
sfdisk $DISK <<-__EOF__
4MiB,${BOOT_SZ}MiB,0xE,*
8MiB,${ROOTFS_SZ}MiB,,-
8MiB,${DATA_SZ}MiB,,-
8MiB,,E,-
,${USER_SZ}MiB,,-
,${MODULE_SZ}MiB,,-
,${RAMDISK_SZ}MiB,,-
,${RAMDISK_RECOVERY_SZ}MiB,,-
,${INFORM_SZ}MiB,,-
__EOF__
else
sfdisk --in-order --Linux --unit M $DISK <<-__EOF__
4,$BOOT_SZ,0xE,*
,$ROOTFS_SZ,,-
,$DATA_SZ,,-
,,E,-
,$USER_SZ,,-
,$MODULE_SZ,,-
,$RAMDISK_SZ,,-
,$RAMDISK_RECOVERY_SZ,,-
,$INFORM_SZ,,-
__EOF__
fi
mkfs.vfat -F 16 ${EFFECTIVE_DISK}1 -n $BOOT
mkfs.ext4 -q ${EFFECTIVE_DISK}2 -L $ROOTFS -F
mkfs.ext4 -q ${EFFECTIVE_DISK}3 -L $SYSTEMDATA -F
mkfs.ext4 -q ${EFFECITVE_DISK}5 -L $USER -F
mkfs.ext4 -q ${EFFECTIVE_DISK}6 -L $MODULE -F
mkfs.ext4 -q ${EFFECTIVE_DISK}7 -L $RAMDISK -F
mkfs.ext4 -q ${EFFECTIVE_DISK}8 -L $RAMDISK_RECOVERY -F
mkfs.ext4 -q ${EFFECTIVE_DISK}9 -L $INFORM -F
# create "reboot-param.bin" file in inform partition for passing reboot parameter
# It should be done only once upon partition format.
umount ${EFFECTIVE_DISK}9
mkdir mnt_tmp
mount -t ext4 ${EFFECTIVE_DISK}9 ./mnt_tmp
touch ./mnt_tmp/reboot-param.bin
sync
umount ./mnt_tmp
rmdir mnt_tmp
}
function show_usage () {
echo "- Usage:"
echo " sudo ./sd_fusing*.sh -d <device> [-b <path> <path> ..] [--format]"
}
function check_partition_format () {
if [ "$FORMAT" != "2" ]; then
echo "-----------------------"
echo "Skip $DEVICE format"
echo "-----------------------"
return 0
fi
echo "-------------------------------"
echo "Start $DEVICE format"
echo ""
mkpart_3
echo "End $DEVICE format"
echo "-------------------------------"
echo ""
}
function check_args () {
if [ "$DEVICE" == "" ]; then
echo "$(tput setaf 1)$(tput bold)- Device node is empty!"
show_usage
tput sgr 0
exit 0
fi
if [ "$DEVICE" != "" ]; then
echo "Device: $DEVICE"
fi
if [ "$FUSING_BINARY_NUM" != 0 ]; then
echo "Fusing binary: "
for ((bid = 0 ; bid < $FUSING_BINARY_NUM ; bid++))
do
echo " ${FUSING_BINARY_ARRAY[bid]}"
done
echo ""
fi
if [ "$FORMAT" == "1" ]; then
echo ""
echo "$(tput setaf 3)$(tput bold)$DEVICE will be formatted, Is it OK? [y/n]"
tput sgr 0
read input
if [ "$input" == "y" ] || [ "$input" == "Y" ]; then
FORMAT=2
else
FORMAT=0
fi
fi
}
function print_logo () {
echo ""
echo "Raspberry Pi3 downloader, version 0.1"
echo ""
}
print_logo
function add_fusing_binary() {
local declare binary_name=$1
FUSING_BINARY_ARRAY[$FUSING_BINARY_NUM]=$binary_name
FUSING_BINARY_NUM=$((FUSING_BINARY_NUM + 1))
}
declare -i binary_option=0
while test $# -ne 0; do
option=$1
shift
case $option in
--f | --format)
FORMAT="1"
binary_option=0
;;
-d)
DEVICE=$1
binary_option=0
shift
;;
-b)
add_fusing_binary $1
binary_option=1
shift
;;
*)
if [ $binary_option == 1 ];then
add_fusing_binary $option
else
echo "Unkown command: $option"
exit
fi
;;
esac
done
check_args
check_partition_format
fuse_image