-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathprepare_selfinst
executable file
·390 lines (348 loc) · 11.7 KB
/
prepare_selfinst
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
printf "\n\033[31m\033[1mScript must be run as root !\033[22m\033[37m\n\n"
exit 1
fi
usage() {
printf "\n\033[33m"
printf "USAGE: prepare_selfinst dest board\n"
printf " \033[1mdest\033[22m destination, block device (/dev/sdX) or image name\n"
printf " \033[1mboard\033[22m Odroid board type: \033[1mc2\033[22m or \033[1mxu4\033[22m\n"
printf "\033[37m\n"
}
needed_packages=""
_testpkg=$(dpkg -l | grep parted)
if [ "${_testpkg}" = "" ]; then
needed_packages="${needed_packages}parted "
fi
_testpkg=$(dpkg -l | grep dosfstools)
if [ "${_testpkg}" = "" ]; then
needed_packages="${needed_packages}dosfstools "
fi
if [ ! "${needed_packages}" = "" ]; then
printf "\n\033[31m\033[1mYou have to install \033[22m\033[37m%s !\033[37m\n\n" "${needed_packages}"
exit 1
fi
if [ "${1}" = "" ]; then
printf "\n\033[31m\033[1mdestination must be specified\033[22m\033[37m\n"
usage
exit 1
fi
if [ ! "${2}" = "xu4" ] && [ ! "${2}" = "c2" ] && [ ! "${2}" = "c1" ]; then
printf "\n\033[31m\033[1mOdroid board type must be \033[33mxu4\033[31m or \033[33mc2\033[31m\033[37m\n"
usage
exit 1
fi
if [ "${2}" = "c2" ]; then
part_start=65536 # RESERVE 32MB AT CARD START
o_device="c2"
src_dir="C2"
elif [ "${2}" = "c1" ]; then
part_start=65536 # RESERVE 32MB AT CARD START
o_device="c1"
src_dir="C1"
else
part_start=131072 # RESERVE 64MB AT CARD START
o_device="xu4"
src_dir="XU4"
if [ -b $sdcard ]; then
printf "\n\033[33m\033[1mWARNING:\033[22m Do not use EMMC in SD adapter to prepare the card!\033[37m\n"
fi
fi
_image_size=250
sdcard=${1}
#====================
image_error_exit() {
echo ""
umount _mnt > /dev/null 2>&1
rmdir _mnt > /dev/null 2>&1
rm -rf tmp/* > /dev/null 2>&1
rmdir tmp > /dev/null 2>&1
printf "\033[31m\033[1m%s\033[37m\033[22m\n" "${1}"
exit 1
}
#====================================================================================
echo ""
printf "\033[36m\033[1m\n"
echo "========================================================"
echo "= Preparing Odroid MultiBoot selfinstall sd card/image ="
echo "========================================================"
printf "\033[22m\033[37m\n"
printf "\033[33mPreparing for \033[1mOdroid-%s\033[22m\033[37m\n\n" "${o_device}"
# =================
# Check destination
# =================
if [ -b $sdcard ]; then
# we are working with block device
# Test if requested drive is removable
ISREMOVABLE=`udevadm info -a -n ${sdcard} | grep -o "ATTR{removable}==\"1\""`
if [ ! "${ISREMOVABLE}" = "ATTR{removable}==\"1\"" ] ; then
printf "\033[31m\033[1m%s IS NOT REMOVABLE DRIVE !, Exiting.\033[37m\033[22m\n" "${sdcard}"
exit 1
fi
_sdok=`fdisk -l $sdcard 2> /dev/null | grep Disk`
if [ "$_sdok" = "" ]; then
printf "\033[31m\033[1m%s NOT FOUND !, Exiting.\033[37m\033[22m\n" "${sdcard}"
exit 1
fi
_image_size=2048
_isimage="no"
else
_tstname=`echo ${sdcard} | grep "/dev"`
if [ ! "${_tstname}" = "" ]; then
printf "\n\033[31m\033[1mWrong image name!\033[22m\033[37m\n\n"
exit 1
fi
_isimage="yes"
fi
# =================================
# Create partition images if needed
if [ "${_isimage}" = "yes" ]; then
echo "Creating image file...."
dd if=/dev/zero of=${sdcard}1 bs=1M count=${_image_size} > /dev/null 2>&1
dd if=/dev/zero of=${sdcard}0 bs=512 count=${part_start} > /dev/null 2>&1
mkfs -t vfat -n SELFINST ${sdcard}1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
image_error_exit " ERROR formating vfat partition."
fi
fi
umount ${sdcard}* > /dev/null 2>&1
sleep 1
# ==========================================================
# Partition and format sd/emmc card if not creating an image
if [ ! "${_isimage}" = "yes" ]; then
printf "\033[31m\033[1mWARNING: SD card \033[36m%s\033[31m WILL BE ERASED !\033[22m\033[37m, Continue (y/N)? " "${sdcard}"
read -n 1 ANSWER
if [ ! "${ANSWER}" = "y" ] ; then
echo "."
image_error_exit "Canceled.."
fi
echo ""
echo "Erasing ${sdcard} ..."
dd if=/dev/zero of=${sdcard} bs=1M count=10 oflag=direct > /dev/null 2>&1
sync
sleep 1
partprobe -s ${sdcard} > /dev/null 2>&1
if [ $? -ne 0 ]; then
image_error_exit "ERROR."
exit 1
fi
sleep 1
echo "Creating new filesystem on $sdcard ..."
echo -e "o\nw" | fdisk ${sdcard} > /dev/null 2>&1
sync
echo " New filesystem created on SD card."
sleep 1
partprobe -s ${sdcard} > /dev/null 2>&1
if [ $? -ne 0 ]; then
image_error_exit "ERROR."
exit 1
fi
sleep 1
echo ""
echo "Creating installer partition on $sdcard..."
echo -e "n\np\n1\n$part_start\n\nt\nc\nw" | fdisk ${sdcard} > /dev/null 2>&1
sleep 1
partprobe -s ${sdcard} > /dev/null 2>&1
if [ $? -ne 0 ]; then
image_error_exit "ERROR."
exit 1
fi
sleep 1
echo ""
echo "Formating partition ..."
dd if=/dev/zero of=${sdcard}1 bs=1M count=1 oflag=direct > /dev/null 2>&1
sync
mkfs -t vfat -F 32 -n SELFINST ${sdcard}1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
image_error_exit " ERROR formating installer partition."
fi
echo " selfinst partition formated."
sync
echo ""
echo "Copying boot section..."
if [ ${o_device} = "xu4" ]; then
dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} bs=512 seek=1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing bl1.bin."
fi
sync
dd if=${src_dir}/initramfs/ramfs/multi/bl2.bin of=${sdcard} bs=512 seek=31 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing bl2.bin."
fi
sync
dd if=${src_dir}/initramfs/ramfs/multi/u-boot.bin of=${sdcard} bs=512 seek=63 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing u-boot.bin."
fi
sync
dd if=${src_dir}/initramfs/ramfs/multi/tzsw.bin of=${sdcard} bs=512 seek=2111 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing tzsw.bin."
fi
sync
elif [ ${o_device} = "c1" ]; then
dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} bs=1 count=442 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing bl1.bin."
fi
dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} bs=512 skip=1 seek=1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing bl1.bin."
fi
sync
dd if=${src_dir}/initramfs/ramfs/multi/u-boot.bin of=${sdcard} bs=512 seek=64 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing u-boot.bin."
fi
sync
# u-boot env erase
dd if=/dev/zero of=${sdcard} seek=1024 count=64 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR erasing u-boot environment."
fi
else
dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} bs=1 count=442 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing bl1.bin."
fi
dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} bs=512 skip=1 seek=1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing bl1.bin."
fi
sync
dd if=${src_dir}/initramfs/ramfs/multi/u-boot.bin of=${sdcard} bs=512 seek=97 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing u-boot.bin."
fi
sync
# u-boot env erase
dd if=/dev/zero of=${sdcard} seek=1440 count=64 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR erasing u-boot environment."
fi
fi
fi
sync
mkdir _mnt > /dev/null 2>&1
echo ""
mount ${sdcard}1 _mnt > /dev/null 2>&1
if [ $? -ne 0 ]; then
image_error_exit "ERROR mounting destination partition ${sdcard}1."
exit 1
fi
#========================================================================
echo "Copying installation files..."
mkdir _mnt/multiboot > /dev/null 2>&1
cp -rf ${src_dir}/multiboot/* _mnt/multiboot > /dev/null 2>&1
if [ $? -ne 0 ]; then
image_error_exit "ERROR installing multiboot files on ${sdcard}1."
exit 1
fi
if [ ${o_device} = "xu4" ]; then
cp _mnt/multiboot/boot.scr.multi _mnt/boot.scr > /dev/null 2>&1
else
cp _mnt/multiboot/boot.ini.multi _mnt/boot.ini > /dev/null 2>&1
fi
if [ $? -ne 0 ]; then
image_error_exit "ERROR installing multiboot ini file on ${sdcard}1."
exit 1
fi
umount _mnt > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "umount ${sdcard}1, ERROR"
else
rmdir _mnt
fi
#========================================================================
#=====================================================================================================================
if [ "${_isimage}" = "yes" ]; then
echo "Creating final image..."
dd if=${sdcard}0 of=${sdcard} > /dev/null 2>&1
dd if=${sdcard}1 of=${sdcard} conv=notrunc oflag=append > /dev/null 2>&1
rm ${sdcard}0 > /dev/null 2>&1
rm ${sdcard}1 > /dev/null 2>&1
echo -e "o\nw" | fdisk ${sdcard} > /dev/null 2>&1
echo -e "n\np\n1\n$part_start\n\nt\nc\nw" | fdisk ${sdcard} > /dev/null 2>&1
if [ ${o_device} = "xu4" ]; then
dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} conv=notrunc bs=512 seek=1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing bl1.bin."
fi
sync
dd if=${src_dir}/initramfs/ramfs/multi/bl2.bin of=${sdcard} conv=notrunc seek=31 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing bl2.bin."
fi
sync
dd if=${src_dir}/initramfs/ramfs/multi/u-boot.bin of=${sdcard} conv=notrunc seek=63 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing u-boot.bin."
fi
sync
dd if=${src_dir}/initramfs/ramfs/multi/tzsw.bin of=${sdcard} conv=notrunc seek=2111 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing tzsw.bin."
fi
sync
elif [ ${o_device} = "c1" ]; then
dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} conv=notrunc bs=1 count=442 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing bl1.bin."
fi
dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} conv=notrunc bs=512 skip=1 seek=1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing bl1.bin."
fi
sync
dd if=${src_dir}/initramfs/ramfs/multi/u-boot.bin of=${sdcard} conv=notrunc bs=512 seek=64 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing u-boot.bin."
fi
sync
# u-boot env erase
dd if=/dev/zero of=${sdcard} conv=notrunc seek=1024 count=64 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR erasing u-boot environment."
fi
else
dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} conv=notrunc bs=1 count=442 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing bl1.bin."
fi
dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} conv=notrunc bs=512 skip=1 seek=1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing bl1.bin."
fi
sync
dd if=${src_dir}/initramfs/ramfs/multi/u-boot.bin of=${sdcard} conv=notrunc bs=512 seek=97 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR installing u-boot.bin."
fi
sync
# u-boot env erase
dd if=/dev/zero of=${sdcard} conv=notrunc seek=1440 count=64 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR erasing u-boot environment."
fi
fi
sleep 1
fi
#=====================================================================================================================
rm -rf tmp/* > /dev/null 2>&1
rmdir tmp > /dev/null 2>&1
echo ""
echo "OK."
printf "\033[36m\033[1m\n"
if [ "${_isimage}" = "yes" ]; then
echo "======================================================="
echo "= Odroid MultiBoot selfinstall sd card image prepared ="
echo "======================================================="
else
echo "================================================="
echo "= Odroid MultiBoot selfinstall sd card prepared ="
echo "================================================="
fi
printf "\033[22m\033[37m\n"
exit 0