-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprepare_sd_image_rescue_vim2_uboots
executable file
·417 lines (327 loc) · 8.14 KB
/
prepare_sd_image_rescue_vim2_uboots
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/bin/sh
## hyphop ##
#= prerape rescue image for sd/usb/mmc disk - vfat - uboots
## usage ./prapare_sd_image_rescue [-r] [ /dev/FLASHDISK ] | [ @remote_host ]
set -e
DEV="$1"
sudo=sudo
mkimage=$(dirname $0)/mkimage2
REBUILD_RESCUE=
DEV2=/dev/mmcblk1
UBOOT_VER=
for a in "$@"; do
case $a in
-r)
REBUILD_RESCUE=1
;;
-1)
DEVNAME=VIM1
;;
-2)
DEVNAME=VIM2
;;
-3)
DEVNAME=VIM3
;;
-3l|-3L)
DEVNAME=VIM3L
;;
-e)
DEVNAME=Edge
;;
*@*)
DEV="$a"
;;
# /dev/mmc*)
# DEV2=$a
# ;;
*)
[ -b "$a" ] && {
DEV="$a"
}
;;
esac
done
[ "$DEVNAME" ] || {
echo "[e] DEVNAME undefined">&2
exit 1
}
CMD(){
echo "[#] $@">&2
"$@"
}
UBOOT_SD=../share/u-boot.sd.bin
UBOOT_SPI=../share/u-boot.spi.bin
TU="/tmp/u-boot-mainline"
UBOOT_BIN="$TU/$DEVNAME.u-boot.spi.bin"
CMD ln -sf $TU/$DEVNAME.u-boot.sd.bin $UBOOT_SD
CMD ln -sf $TU/$DEVNAME.u-boot.spi.bin $UBOOT_SPI
## rebuild rescue image
export UBOOT_BIN=$UBOOT_BIN
export DEVNAME=$DEVNAME
echo "
export UBOOT_BIN=$UBOOT_BIN
export DEVNAME=$DEVNAME
"
[ "$REBUILD_RESCUE" ] && {
#NOCACHE=1
$(dirname $0)/pack_image -r
}
D=/tmp/krescue.sd
D2=/tmp/krescue.sd.tmp
#grep $D /proc/mounts && CMD $sudo umount $D
#CMD $sudo losetup -D
[ -d "$D" ] && rm -rf "$D"
[ -d "$D" ] || mkdir -p "$D"
[ -f $UBOOT_SD ] || {
echo "[e] not found $UBOOT_SD">&2
exit 1
}
P=$PWD
DD=$D.img
DDRN="$(dirname $D)/$DEVNAME.$(basename $D)"
OF=3276 ## 1.6M before
OF=3200 ## 1638400 bytes UBOOT AREA before $((512*32*100))
S=$((36*1024*1024))
#S=$((52*1024*1024))
S=$((64*1024*1024)) # total 64M
BS=512
RESCUE=/rescue
[ -d $D$RESCUE ] || CMD mkdir -p $D$RESCUE
CMD mkdir -p $D.tmp
echo "$(TZ= date)" > $D.tmp/generated.date
echo "$(TZ= date +%s )" > $D.tmp/generated.time
DATE=$(cat $D.tmp/generated.date)
TIME=$(cat $D.tmp/generated.time)
#grep --binary-files=text KRESCUE_INFO -A3 krescue.sd.img
#grep --binary-files=text KRESCUE_INFO -A20 krescue.sd.img | grep --binary-files=text -B20 KRESCUE-INFO
## copy files
[ -d $D$RESCUE/docs ] || CMD mkdir -p $D$RESCUE/docs
CMD cp -L ../docs/README-rescue.md $D
CMD cp -L ../docs/README-rescue* $D$RESCUE/docs
#CMD cp -L ../share/Image $D$RESCUE
CMD cp -L ../share/uInitrd* $D$RESCUE
CMD cp -L ../share/uImage $D$RESCUE
#CMD cp -L ../share/uImage* $D$RESCUE
#CMD cp -L ../share/kvim3_linux.dtb $D$RESCUE/uboot.dtb
#CMD cp -L ../share/kvim3_linux.dtb $D$RESCUE/uboot.dtb
#CMD cp -L ../share/*_linux.dtb $D$RESCUE
#CMD cp -L ../share/linux.dtb $D$RESCUE/linux.dtb
#CMD cp -L ../share/kvim_linux.dtb $D$RESCUE/vim1.dtb
#CMD cp -L ../share/kvim2_linux.dtb $D$RESCUE/vim2.dtb
#CMD cp -L ../share/kvim3_linux.dtb $D$RESCUE/vim3.dtb
#CMD cp -L ../share/kvim3_linux.dtb $D$RESCUE/vim3-a311d.dtb
#CMD cp -L ../share/kvim3_linux.dtb $D$RESCUE/vim3-s922x.dtb
#CMD cp -L ../share/kvim3l_linux.dtb $D$RESCUE/vim3-s905d3.dtb
[ "" ] && {
for f in ../share/krescue-*.dtb; do
[ -f "$f" ] && {
CMD cp -L $f $D$RESCUE
}
done
}
[ "1" ] && {
for f in /tmp/modules/*.dtb; do
[ -f "$f" ] && {
CMD cp -L $f $D$RESCUE
}
done
}
CMD cp -L ../files/splash.bmp.gz $D$RESCUE
CMD cp -L ../files/splash.bmp.2.gz $D$RESCUE
CMD cp -L ../files/splash2.bmp $D/splash.bmp
#CMD cp ../files/*_env.txt $D$RESCUE
CMD cp -L ../share/*_env.txt $D$RESCUE
CMD cp -L ../files/rescue.cmd $D$RESCUE
CMD cp -Lr ../files/extra $D$RESCUE
#../files/extra/cmd2scr.sh $D$RESCUE/rescue.cmd
CMD $mkimage -C none -A arm -T script -d $D$RESCUE/rescue.cmd $D$RESCUE/rescue.scr
cat ../files/boot.ini ../files/rescue.cmd >> $D/boot.ini
CMD mkdir $D/boot
cat ../files/boot.ini ../files/rescue.cmd >> $D/boot/boot.ini
CMD cp $D$RESCUE/rescue.cmd $D$RESCUE/hook.cmd
CMD cp $D$RESCUE/rescue.scr $D$RESCUE/hook.scr
CMD cp $D$RESCUE/rescue.scr $D/boot.scr.uimg
CMD cp $D$RESCUE/rescue.scr $D/boot.scr.uimg
CMD cp $D$RESCUE/rescue.scr $D/s905_autoscript
CMD cp $D$RESCUE/rescue.scr $D/aml_autoscript
CMD cp $D$RESCUE/rescue.scr $D/cfgload # coreelec
#CMD cp ../share/openwrt_khadas_fw_vims.tar.xz $D$RESCUE
[ "" ] && {
for v in vim3 vim3l vim2 vim1; do
for t in sd spi; do
for s in /tmp ../bin; do
U="$s/uboot_$v/u-boot.$t.bin"
[ -f "$U" ] && {
CMD cp "$U" "$D$RESCUE/u-boot.$v.$t.bin"
break
}
done
done
done
}
[ "1" ] && {
for v in VIM3 VIM3L VIM2 VIM1 Edge; do
for t in sd.bin spi.bin bin.gz; do
#for s in /tmp ../bin; do
#U="$s/uboot_vims/u-boot.$v.$t"
#U="$s/uboot-mainline/u-boot.$v.$t"
U="$TU/$v.u-boot.$t"
[ -f "$U" ] && {
case $U in
# *gz)
# gzip -dc "$U" > "$D$RESCUE/u-boot.$v.bin"
# ;;
*)
CMD cp "$U" "$D$RESCUE"
;;
esac
break
}
#done
done
done
}
SPI_IMG=
[ "$SPI_IMG" ] && {
[ -f /tmp/krescue.spi.bin ] && {
CMD cp /tmp/krescue.spi.bin $D$RESCUE/
}
}
SS=$D$RESCUE/scripts
[ -d $SS ] || CMD mkdir -p $SS
CMD cp ../scripts/sd/* $SS
### OFFSET SIZE TOTAL
PART_="${OF} $((S/$BS - $OF)) $((S/$BS))"
echo "$PART_" > $D$RESCUE/generated.part
PPP=$PWD
cd $D
rsync -ra . > $D.tmp/files.list
#rsync -ra . | tail -n+2 $D.tmp/files.list
find -type f -exec md5sum {} \; > $D.tmp/files.md5sum
cd $PPP
cp $D.tmp/files.* $D$RESCUE
echo "# grep ./ /tmp/files.md5sum | sort -d | md5sum | head -c32">&2
#grep ./ /boot/rescue/files.md5sum | sort -d | md5sum
HASH=$(grep ./ /tmp/files.md5sum | sort -d | md5sum | head -c32 | tee $D.tmp/generated.hash)
#sed -i "s/$HASH/hash: $(cat $D.tmp/generated.hash)/" $DD.vfat
mv $D.tmp/generated.* $D$RESCUE
#HASH="hash: --------------------------------"
(
printf "\n##KRESCUE_INFO\n"
echo "hash: $HASH"
echo "date: $DATE"
echo "time: $TIME"
echo "size: $S"
echo "part: $PART_"
echo "builder: hyphop"
printf "\n##KRESCUE-INFO\n"
) > $DD.msg
CMD dd if=/dev/zero bs=$BS count=$((S/$BS - $OF)) of=$DD.vfat
CMD mkfs.vfat -n rescue -i DEADBEEF -m $DD.msg $DD.vfat
CMD mdir -i $DD.vfat ::
PPPP=$PWD
cd $D
CMD mcopy -sp -i $DD.vfat $(ls --group-directories-first .) ::
CMD mdir -i $DD.vfat -/ ::
cd $PPPP
CMD sync
#[ "$TAR" ] && {
# CMD tar -czf $D.tar.gz -C$D .
META="$HASH-$TIME-$S-"
T="${D%.sd}-$META.sd.tar"
echo "[T] $T"
case $TAR in
1|gz)
# tar -cf- -C$D . | gzip -c -9 > $D.tar.gz
CMD tar -cf "$T" -C$D .
CMD gzip -9 "$T"
[ -f "$T" ] && rm "$T"
CMD mv "$T.gz" "$D.tar.gz"
;;
xz)
CMD tar -cf "$T" -C$D .
CMD xz -9 "$T"
[ -f "$T" ] && rm "$T"
CMD mv "$T.xz" "$D.tar.xz"
;;
zstd)
./zstd_header2 "$(basename $T)
$(cat $DD.msg)
" > $D.UPDATE
tar -cf- -C$D . | zstd --ultra -22 -T0 >> $D.UPDATE
# tar -cf- -C$D . | zstd -18 -T0 >> $D.UPDATE
./zstd_header2 "
##KRESCUE##END
" >> $D.UPDATE
;;
sfs)
[ -f $D.sfs ] && rm $D.sfs
mksquashfs $D $D.sfs -comp gzip -b 1M
;;
sfs2)
[ -f $D.sfs ] && rm $D.sfs
mksquashfs $D $D.sfs -comp zstd -b 1M -Xcompression-level 22
;;
"")
;;
*)
echo "[e] undef tar compress">&2
exit 1
;;
esac
#}
CMD fsck.fat $DD.vfat
CMD dd if=/dev/zero count=$OF bs=$BS of=$DD
CMD dd if=$UBOOT_SD bs=$BS conv=notrunc of=$DD
cat $DD.vfat >> $DD
sync
echo "label:dos" | sfdisk -q $DD
echo "$OF, " | sfdisk -q $DD -a
sfdisk --part-type $DD 1 e
sync
#CMD dd if=$UBOOT_SD bs=444 count=1 conv=notrunc of=$DD
md5sum $DD | tee $DD.md5sum
echo "[i] rescue sd image: $DD is ready">&2
ls -Ll1 $DD
[ "$GZ" ] && {
# gzip -c $DD > $DD.gz
# ls -Ll1 $DD.gz
echo "[i] make rel $DDRN">&2
gzip -c $DD > $DDRN.gz
}
echo "[i] maybe write to $DEV">&2
[ -b "$DEV" ] && {
# echo "[i] type Y for allow write to $DEV">&2
# [ "$(read YES)" = "Y" ] || {
# echo "[w] owe wait only Y not $YES">&2
# exit 1
# }
echo "[i] write image $DD -> $DEV flash disk! plz wait...">&2
CMD $sudo dd if=$DD of=$DEV conv=notrunc bs=8192
CMD sync
echo "[i] DONE">&2
exit
}
### !!!! rebuild
case $DEV in
*@*)
TO="root@${DEV#*@}"
echo "[i] remote ssh write image $DD to sd: $DEV2">&2
CMD rsync -av --inplace $DD $TO:$DD && {
echo "
/opt/sd_boot $DD
exit
" | CMD ssh $TO
}
#
# CMD ssh $TO dd of=$DEV2 conv=sync < $DD
echo "[i] DONE">&2
exit
;;
esac
echo "[i] maybe next command for write it">&2
echo "./image2flash /tmp/krescue.upgrade.disk.img /dev/#your_drive " >&2
echo "# or remote write to sd">&2
echo "ssh root@vimu dd of=/dev/mmcblk1 bs=4096 < /tmp/krescue.spi.bin">&2
echo "[i] DONE">&2